2013-07-16 37 views
1

我得到了一個場景,我需要在除IE7以外的所有瀏覽器中加載JavaScript。任何幫助是極大的讚賞。我試過下面的代碼:僅在IE7中禁用javascript

<!--[if gt IE 7]> 
<script type="text/javascript"> 
    //Some script XXX to execute 
</script> 
<![endif]--> 
<!--[if (gt IE 9)|!(IE)]><!--> 
<script type="text/javascript">  
    //Some script XXX to execute 
</script> 
<!--<![endif]--> 
+3

這是什麼話? –

+0

如果您使用jQuery,爲什麼不使用它來檢測瀏覽器版本並執行條件執行。 –

+0

[有條件的評論爲'除了IE8'?](http://stackoverflow.com/questions/1692129/conditional-comment-for-except-ie8) – epascarello

回答

0

試試這個

if ($.browser.msie == true && $.browser.version < 8) { 
      //code for ie 7 
     } 
else 
{ 
     document.write("<script src=\"test.js\">"); 
} 
+1

請注意,$ .browser從版本1.9的jQuery中刪除。 – dsomnus

+0

此問題未使用_jQuery_標記(儘管它用於代碼中) –

1

我覺得這個工作太

<!--[if !(IE 7)]> 
    // tag here 
<![endif]--> 

你也可以做的

<![if !IE]> 
    // script tag 
<![endif]> 

<!--[if (gte IE 8)&(lt IE 7)]> 
    // script tag 
<![endif]--> 

在這裏看到的文檔http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx

組合

我不確定您是否正在加載整個文件或只是幾行腳本。如果是隻有幾行jQuery的方式更容易

0

看起來你已經在正確的線路與conditional comments

<!--[if !(IE 7)]>--><script>alert("I'm not IE7");</script><!--<![endif]--> 

任何非IE瀏覽器,這成爲

<!--[if !(IE 7)]>-->      Comment 
<script>alert("I'm not IE7");</script> Element 
<!--<![endif]-->       Comment 

任何IE瀏覽器,這成爲

<!--[if !(IE 7)]>      If not IE 7 
-->          (continued) AND Comment 
<script>alert("I'm not IE7");</script> Element 
<!--<![endif]-->       Comment AND End if 

從哪裏,如果它是IE 7,它得到剪斷。

<!--[if !(IE 7)]>      If - not rendered 
             SNIP 
<![endif]-->        End If 

你可以減少這種下來,如果你不介意的無效HTML

<![if !(IE 7)]><script>alert("I'm not IE7");</script><![endif]>