How to target Edge browser with javascript

后端 未结 9 736
無奈伤痛
無奈伤痛 2020-12-05 09:26

I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?

I maintain an old product and I

9条回答
  •  眼角桃花
    2020-12-05 10:08

    The snippet that I have here is also copy from SO too, am sorry that I could not give you reference for the original code, but I have modified it so here it is for those of you looking for snippet to check for IE 11 and MS Edge Window 10:

    var get_ie_version = function () {
        var sAgent = window.navigator.userAgent;
        var Idx = sAgent.indexOf("MSIE");
    
        // If IE, return version number.
        if (Idx > 0) {
            return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
        }
        // Condition Check IF IE 11 and or MS Edge
        else if ( !!navigator.userAgent.match(/Trident\/7\./)
            || window.navigator.userAgent.indexOf("Edge") > -1 )
        {
            return 11;
        } else {
            return 0; //It is not IE
        }
    };
    

提交回复
热议问题