Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect browser type using jQuery?

Tags:

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  )}; 
like image 407
sealong Maly Avatar asked Oct 14 '13 02:10

sealong Maly


People also ask

How do you check which browser is being used in jQuery?

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.

Can JavaScript detect browser type?

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.

Which method checks whether the specific features get supported by the browser in jQuery?

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.

How do you check which browser is being used in JavaScript?

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.


1 Answers

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());  
like image 129
Milosz Avatar answered Oct 13 '22 20:10

Milosz