Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created June 3, 2011 15:37
Show Gist options
  • Save gavinblair/1006545 to your computer and use it in GitHub Desktop.
Save gavinblair/1006545 to your computer and use it in GitHub Desktop.
Make back/forward buttons work on jQuery UI tabs.
//change the # in the url when switching tabs
//makes refresh work!
$("#tabsMenu li a").click(function(){
window.location.hash = $(this).attr("href");
});
//make back/forward buttons work on tabs
if($("#tabsMenu").length > 0) {
//prevent scrolling when clicking on a tab
var doit = true;
$("#tabsMenu a").click(function(){
doit = false;
});
$(window).bind('popstate', function() {
//must be asynchronous, because usually popstate happens before the a click event.
setTimeout(function(){
if(doit) {
//only happens if back/forward buttons are used
if(window.location.hash.length > 0) {
$("a[href="+window.location.hash+"]").click();
} else {
$("#tabsMenu li:first a").click();
}
}
doit = true;
}, 0);
});
}
@gavinblair
Copy link
Author

cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment