Thew
Thew

Reputation: 15959

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?

Upvotes: 2

Views: 528

Answers (3)

Rael Max
Rael Max

Reputation: 393

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

=)

Upvotes: 1

DrStrangeLove
DrStrangeLove

Reputation: 11557

try

var hash = window.location.hash;

Upvotes: 1

McHerbie
McHerbie

Reputation: 2995

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.

Upvotes: 4

Related Questions