Check if user is using IE

后端 未结 30 1530
心在旅途
心在旅途 2020-11-22 04:34

I am calling a function like the one below by click on divs with a certain class.

Is there a way I can check when starting the function if a user is using Internet

相关标签:
30条回答
  • 2020-11-22 04:56

    It's several years later, and the Edge browser now uses Chromium as its rendering engine.
    Checking for IE 11 is still a thing, sadly.

    Here is a more straightforward approach, as ancient versions of IE should be gone.

    if (window.document.documentMode) {
      // Do IE stuff
    }
    

    Here is my old answer (2014):

    In Edge the User Agent String has changed.

    /**
     * detect IEEdge
     * returns version of IE/Edge or false, if browser is not a Microsoft browser
     */
    function detectIEEdge() {
        var ua = window.navigator.userAgent;
    
        var msie = ua.indexOf('MSIE ');
        if (msie > 0) {
            // IE 10 or older => return version number
            return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
        }
    
        var trident = ua.indexOf('Trident/');
        if (trident > 0) {
            // IE 11 => return version number
            var rv = ua.indexOf('rv:');
            return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
        }
    
        var edge = ua.indexOf('Edge/');
        if (edge > 0) {
           // Edge => return version number
           return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
        }
    
        // other browser
        return false;
    }
    

    Sample usage:

    alert('IEEdge ' + detectIEEdge());
    

    Default string of IE 10:

    Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
    

    Default string of IE 11:

    Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko 
    

    Default string of Edge 12:

    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0 
    

    Default string of Edge 13 (thx @DrCord):

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 
    

    Default string of Edge 14:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/14.14300 
    

    Default string of Edge 15:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063 
    

    Default string of Edge 16:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299 
    

    Default string of Edge 17:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134 
    

    Default string of Edge 18 (Insider preview):

    Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17730 
    

    Test at CodePen:

    http://codepen.io/gapcode/pen/vEJNZN

    0 讨论(0)
  • 2020-11-22 04:57

    You can use the navigator object to detect user-navigator, you don't need jquery for it, the 4 comments below are already included so this snippet works as expected

    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){ 
     // Do stuff with Internet-Exploders ... :)
    }
    

    http://www.javascriptkit.com/javatutors/navigator.shtml

    0 讨论(0)
  • 2020-11-22 04:57

    I just wanted to check if the browser was IE11 or older, because well, they're crap.

    function isCrappyIE() {
        var ua = window.navigator.userAgent;
        var crappyIE = false;
        var msie = ua.indexOf('MSIE ');
        if (msie > 0) {// IE 10 or older => return version number        
            crappyIE = true;
        }
        var trident = ua.indexOf('Trident/');
        if (trident > 0) {// IE 11 => return version number        
            crappyIE = true;
        }
        return crappyIE;
    }   
    
    if(!isCrappyIE()){console.table('not a crappy browser);}
    
    0 讨论(0)
  • 2020-11-22 04:57

    Necromancing.

    In order to not depend on the user-agent string, just check for a few properties:

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

    Also, this detects the new Chredge/Edgium (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;
    }
    

    And this Safari:

    if(window.safari)
    {
        console.log("Safari, yeah!");
    }
    
    0 讨论(0)
  • 2020-11-22 04:58

    I landed on this page in 2020, and I see that till IE5 all userAgent string have Trident, I'm not sure if they have changed anything. So checking only for Trident in the userAgent worked for me.

    var isIE = navigator.userAgent.indexOf('Trident') > -1;
    
    0 讨论(0)
  • 2020-11-22 04:59

    I think it will help you Here

    function checkIsIE() {
        var isIE = false;
        if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
            isIE = true;
        }
        if (isIE)  // If Internet Explorer, return version number
        {
            kendo.ui.Window.fn._keydown = function (originalFn) {
                var KEY_ESC = 27;
                return function (e) {
                    if (e.which !== KEY_ESC) {
                        originalFn.call(this, e);
                    }
                };
            }(kendo.ui.Window.fn._keydown);
    
            var windowBrowser = $("#windowBrowser").kendoWindow({
                modal: true,
                id: 'dialogBrowser',
                visible: false,
                width: "40%",
                title: "Thông báo",
                scrollable: false,
                resizable: false,
                deactivate: false,
                position: {
                    top: 100,
                    left: '30%'
                }
            }).data('kendoWindow');
            var html = '<br /><div style="width:100%;text-align:center"><p style="color:red;font-weight:bold">Please use the browser below to use the tool</p>';
            html += '<img src="/Scripts/IPTVClearFeePackage_Box/Images/firefox.png"/>';
            html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/chrome.png" />';
            html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/opera.png" />';
            html += '<hr /><form><input type="button" class="btn btn-danger" value="Đóng trình duyệt" onclick="window.close()"></form><div>';
            windowBrowser.content(html);
            windowBrowser.open();
    
            $("#windowBrowser").parent().find(".k-window-titlebar").remove();
        }
        else  // If another browser, return 0
        {
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题