简体   繁体   中英

Read the current pageurl from the url using javascript

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.

尝试

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');
    });
});

=)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM