0

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!

larin555
  • 1,669
  • 4
  • 28
  • 43

4 Answers4

6

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]-->
Paolo del Mundo
  • 2,121
  • 13
  • 18
1

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

Turnip
  • 35,836
  • 15
  • 89
  • 111
1

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>
Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

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);
adeneo
  • 312,895
  • 29
  • 395
  • 388