0 0 Votes

jQuery: Detect Browser (Browser Switch)

Info by Progger99 | 21/07/2012 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 easily detected:

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.');
}

So, you can easily notice with which browser and which version you are dealing at the moment and you can deliver fitting code for the appropriate browser.

ReplyPositiveNegative

Progger99

Show Profile | Message
Avatar
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 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.
10/05/2013 at 23:56

ReplyPositive Negative
Reply

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

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 articles on askingbox.com. How to do.