开发者

Read the current pageurl from the url using javascript

开发者 https://www.devze.com 2023-02-14 00:15 出处:网络
So i have this url, like this: index.html#page:page.php Because i load my pages using AJAX i want to set somekinda hotlink to load that page...

So i have this url, like this:

index.html#page:page.php

Because i load my pages using AJAX i want to set somekinda hotlink to load that page...

'Cause im using this function now:

function getdata(file){

    $('#content').fadeOut('slow', function () { 
        $.get(file, function(data) {
            $('#content').html(data);
            $('#content').fadeIn('slow');
        })
    })

}

And i want to make something like this

// if #page is set and #page isn't empty
getdata('src/'+ that-page);

and in the menu: <a href="#page:contact.php" onclick="getdata('src/contact.php');">Contact</a>

So that the URL than is index.php#page:contact.php and if someone goes to that url that he does this:

// if #page is set and #page isn't empty
    getdata('src/'+ that-page);

开发者_运维技巧I hope it is clear now...

So how can i read what is behind #page: in the URL?


You can get the page's hash as it's stored in window.location.hash.

To parse it and only get the filename that you're looking for, you'll want to do something like this:

// #page:test.php
var page = window.location.hash;
    page = page.replace('#page:', '');

alert(page); // test.php

Also, don't call the variable that-page as it'll fail.


try

var hash = window.location.hash;


And you can reduce calls to selector "#content", saving it in a variable:

$content = $('#content');
$content.fadeOut('slow', function () {
    $.get(file, function(data) {
        $content.html(data);
        $content.fadeIn('slow');
    });
});

=)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号