views:

59

answers:

4

Possible Duplicate:
Detect Internet Explorer 6 or below in jQuery

Is there a way of detecting if the browser is pre IE7 (so IE6 or before) using javascript or jQuery?

I found a similar question on here where the answer was basically "don't do that". Well, I don't care, I still want to :)

+6  A: 

The most effective way:

<!--[if lte IE 6]> insert code here <![endif]-->

If you want to run some JavaScript for only IE 6 and below, put a script block inside that conditional comment.

Reasons to use:

  • only recognised by IE (others see a comment)
  • you can put IE-only hacks that may be invalid syntax and it'll still be valid (it's a comment)
  • you can put IE-only hacks in CSS, HTML and JavaScript all with this syntax
Delan Azabani
This seems to work great, thanks very much!
fearofawhackplanet
No problem; happy to help. :)
Delan Azabani
A: 
init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
        || this.searchVersion(navigator.appVersion)
        || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
},
searchString: function (data) {
    for (var i=0;i<data.length;i++) {
        var dataString = data[i].string;
        var dataProp = data[i].prop;
        this.versionSearchString = data[i].versionSearch || data[i].identity;
        if (dataString) {
            if (dataString.indexOf(data[i].subString) != -1)
                return data[i].identity;
        }
        else if (dataProp)
            return data[i].identity;
    }
},
searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
},
dataBrowser: [
    {
        string: navigator.userAgent,
        subString: "Chrome",
        identity: "Chrome"
    },
    {   string: navigator.userAgent,
        subString: "OmniWeb",
        versionSearch: "OmniWeb/",
        identity: "OmniWeb"
    },
    {
        string: navigator.vendor,
        subString: "Apple",
        identity: "Safari",
        versionSearch: "Version"
    },
    {
        prop: window.opera,
        identity: "Opera"
    },
    {
        string: navigator.vendor,
        subString: "iCab",
        identity: "iCab"
    },
    {
        string: navigator.vendor,
        subString: "KDE",
        identity: "Konqueror"
    },
    {
        string: navigator.userAgent,
        subString: "Firefox",
        identity: "Firefox"
    },
    {
        string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
    },
    {       // for newer Netscapes (6+)
        string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE",
        identity: "Explorer",
        versionSearch: "MSIE"
    },
    {
        string: navigator.userAgent,
        subString: "Gecko",
        identity: "Mozilla",
        versionSearch: "rv"
    },
    {       // for older Netscapes (4-)
        string: navigator.userAgent,
        subString: "Mozilla",
        identity: "Netscape",
        versionSearch: "Mozilla"
    }
],
dataOS : [
    {
        string: navigator.platform,
        subString: "Win",
        identity: "Windows"
    },
    {
        string: navigator.platform,
        subString: "Mac",
        identity: "Mac"
    },
    {
           string: navigator.userAgent,
           subString: "iPhone",
           identity: "iPhone/iPod"
    },
    {
        string: navigator.platform,
        subString: "Linux",
        identity: "Linux"
    }
]

};
BrowserDetect.init();
Jordy
A: 

Get the used browser by:

navigator.userAgent
Florian
A: 

In jQuery, you want the $.browser method.

$.browser.msie returns true if the browser is Internet Explorer.

$.browser.version reports the version, as stated in the browser’s identification string. I’m not sure what various versions of IE report for this, so I’m not sure what you’ll need to check for to identify IEs older than 7.

According to the documentation, $.browser is deprecated as of jQuery 1.3, but there apparently aren’t any plans to remove it.

Paul D. Waite