I want to detect if the user is using IE and Firefox but I cannot find the script.
I have code as below:
$(document).ready(function(e) { $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); if($.browser.chrome){ alert(1); //this work well } else if(//the browser is IE){alert(2);} else if(//the browser is Firefox){alert(3);} //The problem is that I don't know how to write a script for IE and FireFox browser for chrome is work fine )};
The best solution is probably: use Modernizr. Can Modernizr and jQuery Migrate Plugin be used in any of the browsers, like: IE6 and even older browsers, since this is one of the main purposes we use the Browser Detections for.
Browser Detection with JavaScript. If you really must do it, detecting what browser someone is using is easy with JavaScript. JavaScript has a standard object called navigator that contains data about the browser being used.
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.
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.
The best solution is probably: use Modernizr.
However, if you necessarily want to use $.browser property, you can do it using jQuery Migrate plugin (for JQuery >= 1.9 - in earlier versions you can just use it) and then do something like:
if($.browser.chrome) { alert(1); } else if ($.browser.mozilla) { alert(2); } else if ($.browser.msie) { alert(3); }
And if you need for some reason to use navigator.userAgent, then it would be:
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); $.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
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