How to detect IE / Edge using javascript?

I am trying to use javascript to apply a certain style to pages based on the browser that the user uses. I can detect all browsers except IE / Edge. In my code snippet, I'm just trying to detect IE / Edge and apply the style.

Here is my code:

var bodyStyle = document.querySelector("#bodyArea");
if((navigator.userAgent.indexOf("Edge") != -1 ) || (!!document.documentMode == true ))
{
    alert("asdf");
    bodyStyle.style.paddingTop = "500px";
}
else
{
    bodyStyle.style.paddingTop = "300px";
}

When I put a warning in the else section, it gives me a warning, but it does not work with the if part. Therefore, I think that my problem occurs when I try to detect IE / Edge. Let me know if it lies elsewhere. If anyone has any feedback, we will be very grateful. Thanks in advance!

+4
source share
1 answer

script IE/Edge:

if (/MSIE 10/i.test(navigator.userAgent)) {
   // this is internet explorer 10
   window.alert('isIE10');
}

if(/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)){
    // this is internet explorer 9 and 11
    window.location = 'pages/core/ie.htm';
}

if (/Edge\/12./i.test(navigator.userAgent)){
   // this is Microsoft Edge
   window.alert('Microsoft Edge');
}

IE Edge: https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx

+1

Source: https://habr.com/ru/post/1617921/


All Articles