Created
November 15, 2011 18:46
-
-
Save mikevalstar/1367937 to your computer and use it in GitHub Desktop.
Simple hashbang URL handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function loadPage(hash, fn){ | |
| if(!hash || hash == '') | |
| return; // nothing to do | |
| var url = (hash[0] == '#') ? hash.substring(2): hash; | |
| $('#C').html('Loading Content...'); // replace with loading screen code | |
| var callback = function(responseText, textStatus, XMLHttpRequest){ | |
| $(document).attr('title', $(responseText).filter('title').text()); | |
| if(fn) fn(); | |
| } | |
| $('#C').load(url + ' #C', callback); | |
| } | |
| $("a").live("click", function(event){ | |
| var href = $(this).attr("href"); | |
| if(href[0] == "/" | |
| && href.substring(href.length - 4) != '.png' | |
| && href.substring(href.length - 4) != '.jpg' | |
| && href.substring(href.length - 5) != '.jpeg' | |
| && href.substring(href.length - 4) != '.gif' ){ | |
| event.preventDefault(); | |
| window.location.hash = "#!" + href; | |
| } | |
| }); | |
| $(window).bind('hashchange', function(){ | |
| loadPage( location.hash ); | |
| }); | |
| loadPage( location.hash, false ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment