问题
I need a particular script to be triggered in Internet Explorer browsers Only!
I've tried this:
<!--[if IE]>
<script></script>
<![endif]-->
Unfortunately this actually stops the script from being loaded.
EDIT: For everyone asking why I need this: IE makes scrolling extremely jumpy when using some animations. In order to address this I need to implement a script that provides smooth scrolling to IE. I don't want to apply it to other browsers as they don't need it and this script although making the scrolling smoother also makes it a bit unnatural.
回答1:
I'm curious why you specifically need to target IE browsers, but the following code should work if that really is what you need to do:
<script type="text/javascript">
if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
document.write('<script src="somescript.js"><\/script>');
</script>
The first half of the Regex (MSIE \d
) is for detecting Internet Explorer 10 and below. The second half is for detecting IE11 (Trident.*rv:
).
If the browser's user agent string matches that pattern, it will append somescript.js
to the page.
回答2:
If someone still looking at running IE specific Javascript then this code still works tested in IE(11), and non IE browsers(Chrome,Firefox,Edge)
<script type="text/javascript">
if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
document.write('<script src="../nolng/js/ex1IE.js"><\/script>
<script src="../nolng/js/ex2IE.js"><\/script>');
else
document.write('<script src="../nolng/js/ex1.js"><\/script>
<script src="../nolng/js/ex2.js"><\/script>');
</script>
回答3:
You could modify this script to run your IE specific JavaScript:
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
alert('IE ' + parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
else alert('otherbrowser');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答4:
currentScript
is supported in all browsers besides IE
<script>
// self-invoked wrapper for scoping the `document` variable
!function( d ) {
if( !d.currentScript ){
var s = d.createElement( 'script' );
s.src = 'ie.js';
d.head.appendChild( s );
}
}(document)
</script>
The above will conditionally append a script file only for IE browsers.
回答5:
Feature detection is a better approach - I recommend looking into Modernizr to progressively enhance your solution when devices/browsers are capable of supporting it.
This CSS based approach sometimes works too, depending on what you need it for.
Put this in the <head>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->
reference article - http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
回答6:
You can use javascript to detect IE and insert your script dynamicaly:
var ua = window.navigator.userAgent;
if (ua.indexOf("MSIE ") != -1|| !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.setAttribute('src', 'YOUR_JS_SCRIPT.js');
script.setAttribute('type', 'text/javascript');
script.setAttribute('async', 'false');
head.appendChild(script);
}
if you can use jQuery you can use shorter code:
if (ua.indexOf("MSIE ") != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
$.getScript('YOUR_JS_SCRIPT.js');
}
Solution found in this post Check if user is using IE with jQuery
来源:https://stackoverflow.com/questions/29987969/how-to-load-a-script-only-in-ie