Created
February 3, 2011 19:21
-
-
Save conspirator/809996 to your computer and use it in GitHub Desktop.
Just call hijack.init(); on document.ready.
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
var documentPathname = document.location.pathname, | |
hijack = { | |
history: $('html').hasClass('history'), | |
currentLocation: documentPathname, | |
firstLoad: 1, | |
init: function(){ | |
var context = this; | |
// Normal link clicks | |
$('a').live('click',function(e){ | |
context.firstLoad = 0; | |
if (e.which == 2 || e.metaKey || e.ctrlKey) { return true; } // Ensure middle, control and command clicks act normally | |
var $link = $(this); | |
// Handle Exceptions first | |
if ($link.hasClass('paginate') || $link.attr('rel').substring(0,9) == 'shadowbox') { | |
return true; | |
} | |
if (context.history) { | |
// If you're not an external link | |
if ($link.attr('href').substring(window.location.protocol.length,(window.location.host.length)+2+window.location.protocol.length) == '//'+window.location.host || $link.attr('href').substring(0,1) == '/') { | |
history.pushState(null, $link.text(), $link.attr('href')); | |
context.currentLocation = document.location.pathname; //update the currentLocation variable | |
context.grabPage($link.attr('href')); | |
} else { | |
return true; | |
} | |
} else { | |
return true; | |
} | |
return false; | |
}); | |
// Back button | |
window.onpopstate = function() { | |
context.on_popstate(); | |
} | |
}, | |
grabPage: function(url){ | |
// Required for Async GA | |
if (window['_gaq']!=undefined) { | |
var cleaned = url.replace('http://'+window.location.host,''); | |
_gaq.push(['_trackPageview', cleaned]); | |
} | |
$("#content").before("<div class='loader'><img src='/assets/images/loading.gif' alt='loading...' /></div>"); | |
$(".loader").fadeIn(800); | |
$("#content").slideUp(500, function(){ | |
$.ajax({ | |
type: "GET", | |
url: url, | |
success: function(data) { | |
var title = data.match(/<title>(.*?)<\/title>/); | |
$content = $(data).find('#content'); | |
document.title = title[1]; | |
$(".loader").fadeOut(300, function(){ | |
$(this).remove(); | |
}); | |
$('#content').attr('class', $content.attr('class')).html($content.html()).slideDown(500, function(){ | |
if ( document.location.hash ) { | |
var top = $(document.location.hash).offset().top; | |
$('body,html').animate({ | |
scrollTop: top | |
}); | |
} | |
}); | |
$(".nav .active").removeClass('active'); | |
var x = $content.attr('data-navhighlight'); | |
if (x != 'home'){ | |
$("#nav-" + x).addClass('active'); | |
} | |
pageReady(); | |
}, | |
error: function(data) { | |
alert('Something went wrong. Please try again.'); | |
} | |
}); | |
}); | |
}, | |
on_popstate: function(){ | |
if (!this.firstLoad && this.currentLocation != document.location.pathname) { | |
log('on popstate fired'); | |
this.grabPage(document.location.href); | |
} | |
this.currentLocation = document.location; //update the currentLocation variable | |
if ( document.location.hash != "" ) { | |
$('body,html').animate({ | |
scrollTop: $(document.location.hash).offset().top | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment