Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery browser detection?

Is there a good way to detect if the user's browser is Internet Explorer using jQuery?

I have an issue with PNG graphics using IE and want to swap them for GIF's only if the user is viewing the site with IE.

like image 421
Dancer Avatar asked Dec 21 '10 10:12

Dancer


People also ask

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 does modernizr detect browser?

We can access various properties of this object 'Modernizr' for feature detection using “Modernizr. featureName”. For example, Modernizr. video will return “true” if the browser supports the video element, and false if the browser doesn't.

How do I know what browser I am using 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.


2 Answers

You can using $.browser, yes, but it's a bad idea to use browser detection:

if($.browser.msie) { /* IE */ }

A better option for instance would be $.support which is feature detection, like this:

if(!$.support.opacity) { /* IE 6-8 */ }

$.support.opacity is false in browsers that don't support opacity in styling (though IE 7-8 handle transparent PNGs file, so this still isn't ideal, depending on what you're after...personally I think you'd be giving IE 7/8 users a sub-optimal experience).

What you should really do is target IE6 which doesn't support transparent PNGs (without an alpha filter), like this:

<!--[if IE 6]>
  <link rel="stylesheet" type="text/css" href="IE6ImageStyles.css">
<![endif]-->
like image 103
Nick Craver Avatar answered Sep 17 '22 18:09

Nick Craver


Yes, you can, but they prefer you to use jQuery.support: http://api.jquery.com/jQuery.support/.

In this case, use jQuery.support.opacity.

Edit: assuming this is about opacity, of course.

like image 20
Spiny Norman Avatar answered Sep 20 '22 18:09

Spiny Norman