22 Votes

jQuery: Detect Browser (Browser Switch)

Info by Progger99 | 2012-07-21 at 22:36

In jQuery, it is quite easy to detect which browser is currently browsing the page. With this, you can easily and quickly program a browser switch.

Here is a first simple example:

if ($.browser.mozilla) {
    alert('Mozilla Firefox');
}
 
if ($.browser.chrome) {
    alert('Chrome');
}
 
if ($.browser.opera) {
    alert('Opera');
}
 
if ($.browser.safari) {
    alert('Safari');
}
 
if ($.browser.msie) {
    alert('Microsoft Internet Explorer');
}

Also the version of the browser can be detected easily:

if ($.browser.msie && $.browser.version <= 6) {
   alert('You are using Internet Explorer 6 or a lesser version.');
} else {
   alert('You are using a proper browser.');
}

This makes it easy to determine with which browser and which version you are dealing at the moment and you can deliver fitting code for the appropriate browser.

ReplyPositiveNegativeDateVotes
00 Votes

Was removed in jQuery 1.9. Is there any other way?

See: http://api.jquery.com/jQuery.browser/
2013-05-10 at 15:13

ReplyPositive Negative
22 Votes

Unfortunately, today, my described jQuery function $.browser is marked as depreciated and no longer available/removed since jQuery 1.9 so that you can not use this function by simply using the newest version of jQuery. I would like to give you a workaround for this issue with this post.

First suggestion: If you really want to use an easy way of detecting browser using jQuery, the simplest possibility is to use the last working version of jQuery before 1.9 which is still supporting the function:

http://code.jquery.com/jquery-1.8.3.min.js

Otherwise, there is also the possibility to use JavaScript's navigator.userAgent instead:

if (navigator.userAgent.match(/mozilla/i) {
   ...
}
if (navigator.userAgent.match(/webkit/i) {
   ...
}
if (navigator.userAgent.match(/ie/i) {
   ...
}
if (navigator.userAgent.match(/opera/i) {
   ...
}

Another way is using the plugin jQuery Migrate, which gives you backwards-compatibility in new jQuery versions. This plugin is providing all of the functions removed today.
Last update on 2023-11-02 | Created on 2013-05-10

Positive Negative
Reply
Reply

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

jQuery: Show and hide elements

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.