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');
}
});
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');
});
}
To detect user browser information we use the navigator. userAgent property. And then we match with the browser name to identify the user browser. Now call this JS function on page load, and this will display the user browser name on page load.
Specific feature detection checks if a specific feature is available, instead of developing against a specific browser. This way, developers can write their code for two cases: the browser does support said feature, or the browser does not support said feature.
Answer: To establish the actual name of the user's Web browser, you can use the navigator. appName and navigator. userAgent properties. The userAgent property is more reliable than appName because, for example, Firefox or Opera may return the string "Netscape" as the value of navigator.
You should add a little sanity check to your popstate
handler, 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:
$(function(){
$(window).on('popstate', popState);
// call popstate on document ready
$(popstate);
});
The answer suggesting you paste the code from $.browser
back 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 $.browser
is 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 $.browser
back into your code, the jQuery team decided to kill it for a reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With