简体   繁体   English

在访问网页时检测IE的所有版本

[英]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. 我喜欢检测用户何时使用任何IE版本。

does anyone have coding to alert me when it detects IE? 有人检测到IE时会有编码来提醒我吗?

Gordon 高登

You could use the $.browser property: 您可以使用$.browser属性:

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 例如,如果您需要JSON解析器,请转到

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? 为什么要检测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. (我不认为这总是可能的,但几乎总是可能的。)jQuery的jQuery.support提供了许多这样的功能,而链接页面为其提供了通常用于功能检测的强大链接。 Or, as Darin says, you can look at $.browser , but that's really a last resort. 或者,正如Darin所说,您可以查看$.browser ,但这确实是不得已的方法。

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: 撇开浏览器与功能检测的价值判断,您还可以使用IE条件注释:

<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 ... $ .browser.msie在最新版本的jquery中不受支持,您可以添加jquery-migrate-1.2.1.min.js或使用以下jquery函数...对于IE,它也可以为您提供版本...

call currentBrowser().browser to detect browser and currentBrowser().version for IE version ......... 调用currentBrowser()。browser以检测IE版本的浏览器和currentBrowser()。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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM