19

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Dancer
  • 17,035
  • 38
  • 129
  • 206
  • 2
    whats the issue you have with pngs? it would probably be better to use php or something similar server side to swap the graphics depending on browser type. a) more reliable and b) will work for people with no javascript. – Thomas Clayson Dec 21 '10 at 10:05
  • @Thomas Clayson, There is actually no reliable way of doing this server side. – Mattias Jakobsson Dec 21 '10 at 10:18
  • the annoying blue backgrounds in ie6, I have a striped background box and want to use a png over the top as a Gif doesn't display correctly – Dancer Dec 21 '10 at 10:29
  • 2
    Paul... if its easier i've posted some code that I use which acctually gets round the problem with transparent pngs in ie6. Mattias what about $_SERVER['HTTP_USER_AGENT']? and its more reliable because it will work on all browsers with all settings. – Thomas Clayson Dec 21 '10 at 11:34
  • @Thomas Clayson, It isn't reliable as the user can easily change it. – Mattias Jakobsson Dec 21 '10 at 12:32
  • 2
    yeah, but if the user changes it they're looking for a different outcome... you should still base your website around it. If a user changes it they know different things would happen, especially with something like this where you're just looking for ie6. – Thomas Clayson Dec 21 '10 at 12:39
  • From what I have gathered looking at the comments, $.support is recommended but it just looks at the ability to support opacity in styling. What if I need to do specific tasks like display the correct browser logo depending on what browser is being used currently. Can this be achieved using $.support or would browser detection be the desired way to go? – Darth Coder Mar 30 '14 at 17:19

8 Answers8

21

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]-->
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
7

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.

Spiny Norman
  • 8,277
  • 1
  • 30
  • 55
3

$.browser has been removed in 1.9 as it's now suggested feature detection was preferred via $.support

MistereeDevlord
  • 876
  • 7
  • 10
2

For better browser detection jQuery strongly recommend the use of an external library such as Modernizr instead of dependency on properties in jQuery.support or the deprecated jQuery.browser (removed since v1.9)

numediaweb
  • 16,362
  • 12
  • 74
  • 110
1
$.browser.webkit
$.browser.safari
$.browser.opera
$.browser.msie
$.browser.mozilla

if ($.browser.msie) {
        //do something
}
else if ($.browser.mozilla) {
        //do something else
}

works with jQuery 1.3

CodeOverRide
  • 4,431
  • 43
  • 36
  • 1
    "We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery." – neoswf Aug 29 '12 at 21:13
  • 1
    also, your `== true` is redundant. Simply calling `if ($.browser.msie) { }` would work just as well. – Ortund Feb 28 '13 at 14:07
0

Check out the $.browser function.

To detect IE, you can also and better go for IE conditional comments.

Example:

<!--[if IE]>
  Special instructions for IE here
<![endif]-->
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    "We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery." – neoswf Aug 29 '12 at 22:15
0
<script>
    jQuery.each(jQuery.browser, function(i, val) {
      $("<div>" + i + " : <span>" + val + "</span>")
                .appendTo(document.body);
    });</script>

$.browser.msie
for IE

-1

I realise this isn't an answer - but I can't really post this in a comment!

If you're going to use javascript this code fixes the png issue with ie6. I haven't used it much, but afaik you need a transparent gif image called x.gif and it does the rest automatically.

// JavaScript Document

var supersleight    = function() {

    var root = false;
    var applyPositioning = true;

    // Path to a transparent GIF image
    var shim            = 'x.gif';

    // RegExp to match above GIF image name
    var shim_pattern    = /x\.gif$/i;



    var fnLoadPngs = function() { 
        if (root) {
            root = document.getElementById(root);
        }else{
            root = document;
        }
        for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
            // background pngs
            if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
                bg_fnFixPng(obj);
            }
            // image elements
            if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){
                el_fnFixPng(obj);
            }
            // apply position to 'active' elements
            if (applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
                obj.style.position = 'relative';
            }
        }
    };

    var bg_fnFixPng = function(obj) {
        var mode = 'scale';
        var bg  = obj.currentStyle.backgroundImage;
        var src = bg.substring(5,bg.length-2);
        if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
            mode = 'crop';
        }
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
        obj.style.backgroundImage = 'url('+shim+')';
    };

    var el_fnFixPng = function(img) {
        var src = img.src;
        img.style.width = img.width + "px";
        img.style.height = img.height + "px";
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
        img.src = shim;
    };

    var addLoadEvent = function(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            };
        }
    };

    return {
        init: function() { 
            addLoadEvent(fnLoadPngs);
        },

        limitTo: function(el) {
            root = el;
        },

        run: function() {
            fnLoadPngs();
        }
    };
}();

// limit to part of the page ... pass an ID to limitTo:
// supersleight.limitTo('header');

supersleight.init();
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224