Check if user is using IE

后端 未结 30 2059
心在旅途
心在旅途 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: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!");
    }
    

提交回复
热议问题