2011-12-02 131 views
1

我在我的網站上包含了一些jQuery腳本。該腳本不適用於低於IE7的任何內容。現在,我要做到以下幾點:php包含條件註釋

<!--[if !(lt IE 7]> 
    <?php include 'script.html'; ?> 
<![endif]--> 

,如果瀏覽器是「比IE7不低於」它應該只加載腳本。 但它不起作用。

有什麼辦法可以解決這個沒有使用基於PHP的瀏覽器識別?

回答

5

是,建立在瀏覽器識別到你的jQuery/JavaScript的。在JavaScript中,測試能力對象你需要的,如果它不存在,那麼有一些代碼來處理它。

+0

我也建議此。 :) – Timo

+0

完美,非常感謝! – pudelhund

0

幫助第一部分:它不起作用,因爲您不需要在lte之前的(,也不需要!。使用

<!--[if gte IE 7]> 
    <?php include 'script.html'; ?> 
<![endif]--> 

延伸閱讀:Microsoft's page on IE conditional comments

+0

他要求一個非PHP的解決方案。 –

+0

夠公平的。他也指出他的代碼不起作用。我推遲你的回答。 – djb

+0

@Jonathan M OP表示非PHP _browser recognition_解決方案。這不是 - 這是IE的內置條件註釋。 –

0

另一種選擇可以是使用衆所周知的ie7.js腳本,讓年長的IE版本更「標準」的標準,但肯定會令你的jQuery腳本工作。

你可以找到它here

0

你不應該使用瀏覽器探測。相反,使用Feature Detection來測試,如果你的瀏覽器能夠你想要做的事

0
在PHP

你會做以下

$browser = get_browser(null, true); 
if($browser['browser'] == 'MSIE' && $browser['version'] >= 7) { /*CODE*/ 

OR

$u_agent = $_SERVER['HTTP_USER_AGENT']; 
$bname = 'Unknown'; 
$version= ""; 

if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
{ 
    $bname = 'Internet Explorer'; 
} 

// finally get the correct version number 
$known = array('Version', $ub, 'other'); 
$pattern = '#(?<browser>' . join('|', $known) . 
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; 
if (!preg_match_all($pattern, $u_agent, $matches)) { 
    // we have no matching number just continue 
} 

IE的條件語句只有頁面後工作的一個已經被髮送到瀏覽器,以便PHP包含文件將無法正常工作,要包括JS腳本文件中,可以使用

<!--[if !(lt IE 7]> 
<script src='script.js' type='text/javascript'></script> 
<![endif]--> 

爲JavaScript瀏覽器檢測你可以使用:

if(BrowserDetect.browser == "MSIE" && BrowserDetect.version >= '7') { 
    /* CODE */ 
}