One line IE javascript

Is there a way to make only one line of IE-specific javascript.

<script> <!--[if gte IE 7]><!-->SOME IE JS<![endif]--> <!--[else]><!-->NORMAL JS<![endif]--> </script> 
+4
source share
4 answers

You can define a global var in <! - [if ... and then just check it out later.

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

...

 if (old_ie) //whatever you need to do 
+5
source

Option 1

Similar to the code in the original question, but with the conditions in the right place - 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 browser and version. I recommend http://www.quirksmode.org/js/detect.html . If you used the .js file found from the link, you could have something like this:

 <script type="text/javascript"> if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == 7) { //IE 7 } else { //All others } </script> 
+6
source

Check out the "conditional compilation" .

 /*@cc_on @*/ /*@if (@_jscript_version >= 1) alert("Hello from Internet Explorer!"); /*@else @*/ alert("Not Internet Explorer! Yaay!"); /*@end @*/ 

The code after the if part looks like a comment on all browsers except IE.

0
source

Try the following:

 <!--[if IE ]><script>alert('ok')</script> <![endif]--> 
0
source

Source: https://habr.com/ru/post/1387261/


All Articles