views:

1205

answers:

1

Hi there,

I am trying to get a handle on using URL hashes in jQuery to control history in Ajax and make links / pages book-markable. I have tried almost every plug-in out there, and I cant seem to get any to work properly, so I dont have any code examples really. But I am open to any suggestions, information, tutorials, etc.

The difference on one of the pages I am trying to incorporate this into is that I have a jQuery animation driven splash/loading page, which is also the same page that all of the content will be loaded into.

..and on this link, I want to by-pass all of the splash / loading animation and directly load the content based on the hash values / string (in this case, #home).

I've been trying to figure this one out for a while, any help is greatly appreciated, thanks!

+2  A: 

So what are you having problems with? Setting the hash tag or handling the change of the hash?

Of course setting the hashes is simply a question of putting the hashes in links, for example <a href="www.voidsync.com/2010/#page">Link</a> but I'm guessing that's not your problem.

To actually do something with the hash, you must have a listener function that checks for example every 100ms has the hash changed and acts accordingly. A simple function could go like this:

$(function() {
    var current_hash = false;
    setInterval(function() {
        if(window.location.hash != current_hash) {
            current_hash = window.location.hash;
            $('#content').load("content.php?page="+current_hash);
        }        
    }, 100);    
});

That (untested) function would check every 100ms if the hash has changed, and if it has, then updates the page via Ajax.

This function also works on page load, so if user lands on the page with a link such as www.voidsync.com/2010/#images, the function will load the page 'images' automatically. So history and bookmarking works.

Hope that helps, just ask if you meant something else.

Tatu Ulmanen
did a little re-wording and got it to work, also works in every recent browser and I managed to tweak it to reset the splash loading, cheers!
abysslogic
actually, have to do some more rewriting to make the links bookmarkable and bypass the default splash page if the hash is set, but not while the loading animations are functioning.
abysslogic
Mind posting the rewrite abysslogic?
rideon88