开发者

detect all versions of IE when visiting webpage

I am having huge issues in making my website work in multiple browsers. I like to detect when a user is using any IE version.

does anyone have coding to alert me when it d开发者_开发百科etects IE?

Gordon


You could use the $.browser property:

if ($.browser.msie) {
    alert('IE !');
}


if( /msie/.test(navigator.userAgent.toLowerCase()) ) {
    // omg it's an internet explorer
}

This is the real quick&dirty solution. Anyway you should do feature detection instead of browser detection. For instance, if you need a JSON parser, go for

if( 'JSON' in window ) {}

This would be the correct solution. To check for a browser (maybe even the version also) is not realiable.


Why do you want to detect IE? Feature detection, rather than browser sniffing, is the preferred way to deal with things whenever possible. (I don't think it's always possible, but it's almost always possible.) jQuery's jQuery.support offers a number of these and the linked page for it offers great links to feature detection in general. Or, as Darin says, you can look at $.browser, but that's really a last resort.

If you post questions with the actual problems you're having cross-browser, odds are pretty high people can help you address them in a way that doesn't rely on browser sniffing.


Putting aside value judgments of browser versus feature detection, you can also use IE conditional comments:

<script type="text/javascript">
var isIE = false;
</script>
<![if IE]> <!--- only runs in IE --->
    <script type="text/javascript">isIE = true;</script>
<![endif]>


$.browser.msie is not supported in latest version of jquery you can either add jquery-migrate-1.2.1.min.js or use following jquery function... for IE it also gives you version ...

call currentBrowser().browser to detect browser and currentBrowser().version for IE version .........

function currentBrowser() {

$.returnVal = "";

var browserUserAgent = navigator.userAgent;

if (browserUserAgent.indexOf("Firefox") > -1) {

    $.returnVal = { browser: "Firefox" };
}

else if (browserUserAgent.indexOf("Chrome") > -1) {

    $.returnVal = { browser: "Chrome" };
}

else if (browserUserAgent.indexOf("Safari") > -1) {

    $.returnVal = { browser: "Safari" };
}

else if (browserUserAgent.indexOf("MSIE") > -1) {

    var splitUserAgent = browserUserAgent.split(";");

    for (var val in splitUserAgent) {

        if (splitUserAgent[val].match("MSIE")) {

            var IEVersion = parseInt(splitUserAgent[val].substr(5, splitUserAgent[val].length));
        }
    }

    $.returnVal = { browser: "IE", version: IEVersion };
}

else if (browserUserAgent.indexOf("Opera") > -1) {

    $.returnVal = { browser: "Opera" };
}

else {
    $.returnVal =
     { browser: "other" };
}

return $.returnVal;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜