如何检测IE和Edge浏览器?

时间:2015-10-15 15:26:38

标签: javascript browser-detection

无法在IE或Microsoft Edge中使Parallax正常工作。我查看了论坛,但没有找到问题的解决方案。我现在想出了一个解决方案。如果用户使用IE或Edge,我想显示一条消息。不知道我怎么能检测到正在使用的浏览器是其中之一。

以下是我正在尝试使用的一些JavaScript代码:

<script src="https://github.com/ded/bowser/blob/master/src/bowser.js"></script>

    // Determine Browser Used
browser = require('bowser').browser; becomes browser = require('bowser');
if (bowser.msie || bowser.msedge) {
  alert('Hello Microsoft User');
}

如果有更好的解决方案,我们将不胜感激。

http://peaceandplentyinn.mybnbwebsite.com

3 个答案:

答案 0 :(得分:68)

我怀疑你真的需要检测浏览器。但无论如何它都是(不需要使用库):

// detect IE8 and above, and edge
if (document.documentMode || /Edge/.test(navigator.userAgent)) {
    alert('Hello Microsoft User!');
}

答案 1 :(得分:2)

对我来说更好:

var uA = window.navigator.userAgent,
    isIE = /msie\s|trident\/|edge\//i.test(uA) && !!(document.uniqueID || document.documentMode || window.ActiveXObject || window.MSInputMethodContext),
    checkVersion = (isIE && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;

开始运行:http://jsfiddle.net/Webnewbie/apa1nvu8/

答案 2 :(得分:1)

我使用这些功能,即使用户代理设置为其他功能也可以使用。

if (document.documentMode) 
{
    console.log('Hello Microsoft IE User!');
}

if (!document.documentMode && window.msWriteProfilerMark) {
    console.log('Hello Microsoft Edge User!');
}

if (window.msWriteProfilerMark) 
{
    console.log('Hello Microsoft User!');
}

这会检测到Chredge / Edgium(又名阿纳海姆)

function isEdg()
{ 

    for (var i = 0, u="Microsoft", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}

这会检测铬:

function isChromium()
{ 

    for (var i = 0, u="Chromium", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}
相关问题