Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line of IE specific javascript

Is there a way to do have only one line of IE specific javascript.

<script>
    <!--[if gte IE 7]><!-->SOME IE JS<![endif]-->
    <!--[else]><!-->NORMAL JS<![endif]-->
</script>
like image 340
Alexis Avatar asked Dec 21 '22 04:12

Alexis


2 Answers

Option 1

Similar to the code in your original question, but with the conditions in the proper location - outside the script block:

<!--[if lt IE 7]>
    <script type="text/javascript">
        //Your one line of code
    </script>
<![endif]-->

<script type="text/javascript">
    //All other script
</script>

Option 2

Use browser detection to check the browser and version. I recommend http://www.quirksmode.org/js/detect.html. If you used the .js file found at the link, you could so something like this:

<script type="text/javascript">
    if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == 7) {
        //IE 7
    }
    else {
        //All others
    }
</script>
like image 149
James Hill Avatar answered Dec 24 '22 01:12

James Hill


You could define a global var in an <!--[if... and then just check for it later.

<!--[if lt IE 7]>
<script type="text/javascript">
    old_ie = true;
</script>
<![endif]-->

...

if (old_ie)
    //whatever you need to do
like image 33
a sad dude Avatar answered Dec 24 '22 01:12

a sad dude