Created
June 3, 2011 15:37
-
-
Save gavinblair/1006545 to your computer and use it in GitHub Desktop.
Make back/forward buttons work on jQuery UI tabs.
This file contains 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
//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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool, thanks!