Skip to content

Instantly share code, notes, and snippets.

@mikevalstar
Created November 15, 2011 18:46
Show Gist options
  • Select an option

  • Save mikevalstar/1367937 to your computer and use it in GitHub Desktop.

Select an option

Save mikevalstar/1367937 to your computer and use it in GitHub Desktop.
Simple hashbang URL handler
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