2012-08-07 29 views
3

我在我的項目中使用了要求JS,它是加載jQuery和一些其他與整個網站和所有瀏覽器相關的JavaScript文件。使用條件註釋與RequireJS只加載IE7/8 jQuery

但是,我需要在Internet Explorer 7上使用一些有條件的jQuery 8 & 8,我試過把它放在頁面的頭部,腳本似乎無法找到jQuery,我假設這是因爲它在jQuery之前被加載。

<script data-main="/@ViewBag.ScriptsFolder/main" src="/scripts/require.js" type="text/javascript"></script> 
    <!--[if (gte IE 6)&(lte IE 8)]> 
     <script type="text/javascript" src="/Scripts/ie.js"></script> 
    <![endif]--> 

有什麼辦法可以糾正嗎?我也嘗試以這種方式加載Selectivizr,這是因爲同樣的問題而不工作。

感謝

回答

16

您可以在html元素使用條件IE特有的類,然後爲它檢查和裝載您現有main腳本中的IE瀏覽器的依賴。因此,該文件的頂部看起來像:

<!--[if lt IE 7]> <html class="ie lt-ie9 lt-ie8 lt-ie7"> <![endif]--> 
<!--[if IE 7]> <html class="ie lt-ie9 lt-ie8"> <![endif]--> 
<!--[if IE 8]> <html class="ie lt-ie9"> <![endif]--> 
<!--[if gt IE 8]> <html class="ie"> <![endif]--> 
<html> 

然後裏面main.js,你可以使用:

if ($('html.lt-ie9').size()) { 
    require(['/Scripts/ie'], function(ieScript) { 
     // ... do stuff 
    }); 
} 

你不能使用你因爲長相require.js描述的方法對於單個data-main屬性來指定'入口點' - 任何後續調用require都應該在Javascript中完成。

+0

非常感謝!我已經使用了這個變種,它完美的作品:-) – hcharge 2012-08-07 13:28:26