简体   繁体   English

如何使用javascript检测IE / Edge?

[英]How to detect IE/Edge using javascript?

I am trying to use javascript to apply a certain style to the pages based on which browser the user is using. 我正在尝试使用javascript根据用户使用的浏览器将某种样式应用于页面。 I can detect all of the browsers except for IE/Edge. 我可以检测除IE / Edge之外的所有浏览器。 In my code snippet I am just trying to detect IE/Edge and apply the style. 在我的代码片段中,我只是尝试检测IE / Edge并应用该样式。

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 an alert in the else section it gives me an alert, but it doesn't work on the if part. 当我在else部分发出警报时,它会给我一个警报,但它对if部分不起作用。 So I think my problem is occurring when I try to detect IE/Edge. 所以当我尝试检测IE / Edge时,我认为我的问题正在发生。 Or if it lay elsewhere, let me know. 或者,如果它位于别处,请告诉我。 If anyone has any feedback, it will be greatly appreciated. 如果有人有任何反馈意见,将不胜感激。 Thanks in advance! 提前致谢!

You can use this custom script to detect IE/Edge: 您可以使用此自定义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');
}

Check out this page for the latest IE and Edge user agent strings: https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx 查看此页面以获取最新的IE和Edge用户代理字符串: https//msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM