If Browser is Internet Explorer: run an alternative script instead

╄→гoц情女王★ 提交于 2019-12-17 06:39:33

问题


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.

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?

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


回答1:


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

If your JS is unobtrusive, you can just use:

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



回答2:


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

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



回答3:


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:

<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>



回答4:


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

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



回答5:


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




回答6:


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.

    <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> 



回答7:


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

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



回答8:


Note that you can also determine in pure js in what browser you script is beeing executed through : 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




回答9:


Test it in all browsers:

<![if !IE]><script type="text/javascript">BROWSERIE = false;</script><![endif]>
<!--[if IE]><script type="text/javascript">BROWSERIE = true;</script><![endif]-->

The first conditional comment works on good browsers, the second one works on IE. That's a global var. Then you can access it from any part, or any JS included in your page, then test it, like:

console.log('isIE = ' + BROWSERIE);
console.log(typeof BROWSERIE);
if(BROWSERIE) $('.modal').removeClass('fade'); // for example, if `fade` causes an error in IE.



回答10:


See this script in Microsoft's developer archives: 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.




回答11:


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

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


来源:https://stackoverflow.com/questions/4411551/if-browser-is-internet-explorer-run-an-alternative-script-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!