Javascript 如果浏览器是 Internet Explorer:改为运行替代脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4411551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:18:39  来源:igfitidea点击:

If Browser is Internet Explorer: run an alternative script instead

javascriptbrowser-detection

提问by dimmy4

I'm using an image carousel script that is quite heavy on the browser. It works great in Opera and Chrome, half decent in FF and absolutely breaks my balls in IE. So i'd like to give IE users an alternative of simple HTML without any action/JS.

我正在使用一个在浏览器上非常繁重的图像轮播脚本。它在 Opera 和 Chrome 中运行良好,在 FF 中表现不错,在 IE 中绝对让我大吃一惊。所以我想给 IE 用户一个没有任何动作/JS 的简单 HTML 的替代方案。

The script doesn't use MT or jQuery and its like 380 lines of JS. Would it be possible to give IE users a plain HTML alternative?

该脚本不使用 MT 或 jQuery 以及类似的 380 行 JS。是否可以为 IE 用户提供纯 HTML 替代方案?

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { // what command can i use? }

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { // what command can i use? }

回答by Flack

This article is quite explanatory: http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx.

这篇文章很有解释性:http: //msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

If your JS is unobtrusive, you can just use:

如果你的 JS 不显眼,你可以使用:

<![if !IE]>
   <script src...
<![endif]>

回答by Seth

You can do something like this to include IE-specific javascript:

您可以执行以下操作以包含特定于 IE 的 javascript:

<!--[IF IE]>
    <script type="text/javascript">
        // IE stuff
    </script>
<![endif]-->

回答by Y.Puzyrenko

For IE10+ standard conditions don't work cause of engine change or some another reasons, cause, you know, it's MSIE. But for IE10+ you need to run something like this in your scripts:

对于 IE10+ 标准条件不起作用的原因是引擎更改或其他一些原因,原因,你知道,它是 MSIE。但是对于 IE10+,您需要在脚本中运行如下内容:

if (navigator.userAgent.match(/Trident\/7\./)) {
  // do stuff for IE.
}

回答by Ender

You define a boolean value with default of true, and then inside an IE conditional comment, set the value to false, and use the value of this to determine whether your advanced code should run. Something like:

您定义一个默认为 true 的布尔值,然后在IE 条件注释中,将该值设置为 false,并使用 this 的值来确定您的高级代码是否应该运行。就像是:

<script type="text/javascript">var runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
    runFancy = false;
    //any other IE specific stuff here
</script>
<![endif]-->
<script type="text/javascript">
    if (runFancy) {
         //do your code that works with sane browsers
    }
</script>

回答by MayorMonty

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { document.write("Your html for IE") }

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { document.write("Your html for IE") }

回答by dimmy4

Here is the script i used and it works like a charm. I used the boolean method Ender suggested as the other ones using only the IE specific script adds something to IE but doesn′t take the original code out.

这是我使用的脚本,它就像一个魅力。我使用了 Ender 建议的布尔方法,因为其他只使用 IE 特定脚本的方法会向 IE 添加一些东西,但不会删除原始代码。

    <script>runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
    runFancy = false;
 </script> // <div>The HTML version for IE went here</div>
<![endif]-->

    // Below is the script used for all other browsers:
    <script src="accmenu/acac1.js" charset="utf-8" type="text/javascript"></script><script>ac1init_doc('',0)</script> 

回答by Péter Zajácz

Try this: The systemLanguage and the userLanguage is undefined in all browser.

试试这个: systemLanguage 和 userLanguage 在所有浏览器中都是未定义的。

if(navigator.userLanguage !== "undefined" && navigator.systemLanguage !== "undefined" && navigator.userAgent.match(/trident/i)) {
    alert("hello explorer i catch U :D")
  }

回答by ?abojad

Note that you can also determine in pure js in what browser you script is beeing executed through : window.navigator.userAgent

请注意,您还可以在纯 js 中确定正在通过哪个浏览器执行脚本:window.navigator.userAgent

However, that's not a recommended way as it's configurable in the browser settings. More info available there: https://developer.mozilla.org/fr/docs/DOM/window.navigator.userAgent

但是,这不是推荐的方式,因为它可以在浏览器设置中进行配置。更多信息可用:https: //developer.mozilla.org/fr/docs/DOM/window.navigator.userAgent

回答by basitmate

See this script in Microsoft's developer archives: https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

在 Microsoft 的开发人员档案中查看此脚本:https: //msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

I have used this script in quite a few projects, never had any problems.

我已经在很多项目中使用过这个脚本,从来没有出现过任何问题。

回答by basitmate

this code works well on my site because it detects whether its ie or not and activates the javascript if it is its below you can check it out live on ie or other browser Just a demo of the if ie javascript in action

这段代码在我的网站上运行良好,因为它检测它是否是 ie 并激活 javascript,如果它在下面,你可以在 ie 或其他浏览器上实时查看它只是 if ie javascript 的演示在行动

<script type="text/javascript">
<!--[if IE]>
window.location.href = "http://yoursite.com/";
<![endif]-->
</script>