I'm trying to change the SRC
attribute of my iFrame for users on IE 8 or less.
Here's my code :
<script>
var i_am_old_ie = false;
<!--[if lte IE 8]>
i_am_old_ie = true;
<![endif]-->
</script>
<script type="text/javascript">
$(document).ready(function() {
if(i_am_old_ie) {
$("#shop").attr("src","shopping_cart/browser_support.html");
} else {
$("#shop").attr("src","shopping_cart/index.html");
}
});
</script>
It detects that it's IE...but still sends all IE users to shopping_cart/browser_support.html
. Even if I use IE 9, it will send me there. Same thing for 7, or 8.
But it sends all other users not using IE to shopping_cart/index.html
(wich is correct).
What's wrong in my code?
Thank you!
You can't insert the inside the script tag. It needs to be outside.
<script>
var i_am_old_ie = false;
</script>
<!--[if lte IE 8]>
<script>
i_am_old_ie = true;
</script>
<![endif]-->
As others mentioned you can not use HTML conditional statements between <script>
tags.
This should work however:
if (document.all && !document.getElementsByClassName) {
i_am_old_ie = true;
}
The above will only run in IE8 or below
Edit:
Some nice examples LINK
There is a different syntax for conditional comments inside JavaScript. What does @cc_on mean in JavaScript? has details.
Wikipedia's Conditional comment page has an example of version switching using it.
<script> /*@cc_on @if (@_jscript_version == 10) document.write("You are using IE10"); @elif (@_jscript_version == 9) document.write("You are using IE9"); @elif (@_jscript_version == 5.8) document.write("You are using IE8"); @elif (@_jscript_version == 5.7 && window.XMLHttpRequest) document.write("You are using IE7"); @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest)) document.write("You are using IE6"); @elif (@_jscript_version == 5.5) document.write("You are using IE5.5"); @else document.write("You are using IE5 or older"); @end @*/ </script>
Conditional HTML comments only work in HTML.
JavaScript is not HTML.
It's generally not a good idea, and feature detection is reccomended, but if using jQuery you can do:
var i_am_old_ie = $.browser.msie && ($.browser.version <= 8);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.