In earlier versions, I used to test if I should be triggering popstate manually on page load, because Chrome triggers it right after load, and Firefox and IE do not.
if ($.browser.mozilla || $.browser.msie) {
$(window).trigger('popstate');
}
Now that they dropped the browser object in 1.9, how should I test for these browsers? Or how do I figure if I need to popstate on page load or not?
The code is:
$(function(){
$(window).on('popstate', popState);
// manual trigger loads template by URL in FF/IE.
if ($.browser.mozilla || $.browser.msie) {
$(window).trigger('popstate');
}
});
Update
Went for this:
function popState(e){
var initial = e.originalEvent === undefined || e.originalEvent.state === null;
if(!initial){
activateRoute({
key: e.originalEvent.state.key,
settings: e.originalEvent.state.settings
},'replace');
}
}
function init(){
$(window).on('popstate', popState);
$(function(){
var route = getRoute(document.location.pathname);
activateRoute(route, 'replace');
});
}
You should add a little sanity check to your
popstatehandler, and make sure that it doesn’t do anything expensive if you “pop” into the same state you started in. Then you can not care about the browser, and instead just call your popstate on document ready:The answer suggesting you paste the code from
$.browserback into your environment is way overkill to support a bad practice. You can feature detect 99% of the things you need to. Almost every use of$.browseris a dangerous. There are almost always ways to detect that.The JavaScript community has been against browser sniffing for a long time. Here is a post from 2009 telling us why it’s a bad idea. There are many others.
I beg you not to copy
$.browserback into your code, the jQuery team decided to kill it for a reason.