开发者

Find out if browser is IE6 or before [duplicate]

This question already has answers here: Closed 12 years ago.

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 :)


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


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();


Get the used browser by:

navigator.userAgent


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜