How do I detect IE and Edge browser?

前端 未结 3 690
难免孤独
难免孤独 2020-12-13 19:01

Can\'t get Parallax working properly in IE or Microsoft Edge. I\'ve looked in forums and haven\'t found a solution to the problem. I\'ve come up with hopefully a solution fo

相关标签:
3条回答
  • 2020-12-13 19:46

    I doubt you really need to detect the browser. But here it is anyway (don't really need to use a library):

    // detect IE8 and above, and edge
    if (document.documentMode || /Edge/.test(navigator.userAgent)) {
        alert('Hello Microsoft User!');
    }
    
    0 讨论(0)
  • 2020-12-13 19:54

    I use these functions, which work even if the user agent is set to something else.

    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!');
    }
    

    And this detects Chredge/Edgium (aka. Anaheim)

    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;
    }
    

    And this detects Chromium:

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 19:58

    For me better this:

    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;
    

    Go run: http://jsfiddle.net/Webnewbie/apa1nvu8/

    0 讨论(0)
提交回复
热议问题