2010-06-29 43 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代码。

相关问题