如何使用JavaScript检测IE / Edge? [英] How to detect IE/Edge using javascript?

查看:147
本文介绍了如何使用JavaScript检测IE / Edge?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用javascript将特定样式应用到基于用户使用的浏览器的页面上。我可以检测除IE / Edge以外的所有浏览器。在我的代码片段中,我只是试图检测IE / Edge并应用样式。



以下是我的代码:

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

当我在else部分中发出警报时,它会给我一个警报,但它不适用于if部分。所以我认为我的问题在我尝试检测IE / Edge时发生。或者如果它位于其他地方,请告诉我。如果有人有任何反馈,将不胜感激。您可以使用此自定义脚本来检测IE / Edge:

  if(/ MSIE 10 / i.test(navigator.userAgent)){
//这是Internet Explorer 10
window.alert('isIE10');


if(/ MSIE 9 / i.test(navigator.userAgent)|| /rv:11.0/i.test(navigator.userAgent)){
//这是Internet Explorer 9和11
window.location ='pages / core / ie.htm';
}

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

查看此页面查看最新的IE和Edge用户代理字符串: https://msdn.microsoft.com/en-us /library/hh869301%28v=vs.85%29.aspx


I am trying to use javascript to apply a certain style to the pages based on which browser the user is using. I can detect all of the browsers except for IE/Edge. In my code snippet I am 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 an alert in the else section it gives me an alert, but it doesn't work on the if part. So I think my problem is occurring when I try to detect 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:

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

这篇关于如何使用JavaScript检测IE / Edge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆