Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to load a script in IE?

It's possible to load a script only in IE with conditional comments

<!--[if lte IE 7]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->

but what if I don't want to load it in IE lte 7 (but still need it in all other browsers)?

Any simple solutions?

P.S. I have a problem with SyntaxHighlighter - too many code slows IE7 down and since I'm short of time, I decided just to turn it off in IE7 for now.

like image 465
Daniel J F Avatar asked Mar 31 '11 19:03

Daniel J F


People also ask

How do I run an Internet Explorer script?

Internet Explorer Click Tools > Internet Options. Click the Security tab > Custom Level. In the Scripting section, click Enable for Active Scripting. In the dialog box that displays, click Yes.

Where do you keep script tags?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.

Are script tags blocking?

Under normal circumstances, a script tag causes the browser to halt rendering, load a file, and run the code. The browser is blocked from doing other useful work because your JavaScript could write to the page, modify existing elements, or redirect to another URL.


6 Answers

This syntax works good (the script wouldn't be commented in firefox, chrome and so on):

<!--[if !IE]><!-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!--<![endif]-->
like image 153
hdg700 Avatar answered Oct 05 '22 06:10

hdg700


This post says you can use the ! (NOT) operator like [if !IE]

like image 43
Bala R Avatar answered Oct 05 '22 04:10

Bala R


<![if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]>
like image 38
Mike Ruhlin Avatar answered Oct 05 '22 04:10

Mike Ruhlin


<!--[if gte IE 7]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
<!--[if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
like image 31
yzxben Avatar answered Oct 05 '22 04:10

yzxben


You could try detecting the browser server-side and then echo the appropriate script includes.

The following has an example on simplistic browser detection in PHP:

http://www.php-scripts.com/20050912/12/

like image 21
Justin Pearce Avatar answered Oct 05 '22 06:10

Justin Pearce


Since conditional statements are not Working in IE (10,11) and only IE(11) is supported by Microsoft and if anyone is 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>');}
    else 
        {document.write('<script src="../nolng/js/ex1.js"><\/script>'); // for Chrome,Firefox,Edge}
</script>
like image 22
Vikram Avatar answered Oct 05 '22 04:10

Vikram