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); | |
}); | |
} |
I was adding back/forward functionality to a site that already had UI tabs. A looping check for the hash would be slower, but you're right, it would work in all browsers.
This worked for me:
http://benalman.com/code/projects/jquery-bbq/examples/fragment-jquery-ui-tabs/
cool, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You don't need to watch for the popstate command I don't think, you just do a looping check of the hash to see if it has changed and then change the page contents accordingly