2010-06-29 44 views
1

我有我要加載的頁面不同的內容對於不同的瀏覽器如果IE然後包含文件[A] else include file [B]

實施例:

IF Internet explorer 

{include file="/content1.tpl"} 

ELSE if any other browser 

    {include file="/content2.tpl"} 

{/if} 

Content1.tpl & 內容2.tpl是具有其推崇的Html & CSS兩個不同的文件。

我該如何使用Javascript或PHP來實現這一目標?

感謝,

  • Mandar

編輯

我要的是,IE完全忽視content2.tpl

& Mozilla的或其他完全忽視content1.tpl

+5

**不要依賴用戶代理字符串** !!!非常糟糕的做法。 – galambalazs 2010-06-29 15:11:55

回答

2

這項工作:

<!--[if IE]> 
{include file="/content1.tpl"}  
<![endif]--> 

<![if !IE]> 
{include file="/content2.tpl"} 
<![endif]> 

HERE瞭解更多詳情。

2

在PHP中,你可以看到一個名爲$ _SERVER超全局。在那裏,您可以在HTTP_USER_AGENT鍵下找到瀏覽器。

這將被計算在服務器端,使用PHP和Smarty。

在Javascript中,使用this

在HTML中你可以使用this syntax

10

不要爲此使用PHP。在瀏覽器級別執行此操作更安全,最好使用conditional comments。你cannot trust the HTTP_USER_AGENT,因爲它很容易僞造/改變,所以你不能自信地(因此不應該)根據它的價值做出決定。堅持一個.tpl文件,幷包含基於條件註釋的特定樣式表或使用這些註釋添加附加標記。例如,您可以添加額外的類似這樣的標記,然後相應目標:

<html> 
<body> 
<!--[if IE 6]><div id="ie6"><![endif]--> 
... main content here 
<!--[if IE 6]></div><![endif]--> 
</body> 
</html> 
+0

我想要的是,IE完全忽略content2.tpl &Mozilla或其他完全忽略content1.tpl – MANnDAaR 2010-06-29 15:25:26

+0

@manndaar - 雖然沒有可靠的方法來做到這一點 – robjmills 2010-06-29 15:29:31

0

這應該工作:

<script type = " javascript" > 
if (navigator.appName == "Microsoft Internet Explorer") 
{ 
    /* 
     do something 
    */ 
} 
else 
{ 
    /* 
     do something else 
    */ 
} 
</script> 
2
<!--[if IE]> 
{include file="/content1.tpl"}  
<![endif]--> 

<comment> 
{include file="/content2.tpl"} 
</comment> 

Internet Explorer忽略<comment></comment>標記,就像它們是標準註釋一樣,但所有其他瀏覽器都像標準代碼一樣閱讀它們。這非常適合隱藏非IE代碼。

相關問題