【问题标题】:If IE then include file [A] else include file [B]如果 IE 则包含文件 [A] 否则包含文件 [B]
【发布时间】:2010-06-29 15:04:47
【问题描述】:

我想要为不同的浏览器加载不同的内容的页面。

示例:

IF Internet explorer

{include file="/content1.tpl"}

ELSE if any other browser

   {include file="/content2.tpl"}

{/if}

Content1.tplContent 2.tpl 是两个不同的文件,具有各自的 Html 和 CSS。

如何使用 Javascript 或 php 实现这一点?

谢谢,

  • 曼达

编辑

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

& Mozilla 或其他完全忽略 content1.tpl

【问题讨论】:

  • 不要依赖用户代理字符串!!!非常糟糕的方法。

标签: php javascript smarty


【解决方案1】:

不要为此使用 PHP。 在浏览器级别这样做会更安全,最好使用conditional comments。你cannot trust the HTTP_USER_AGENT 因为它很容易伪造/更改,所以你不能自信地(因此不应该)根据它的价值做出决定。坚持一个.tpl 文件,或者包含基于条件注释的特定样式表,或者使用这些cmets 添加额外的标记。例如,您可以像这样添加额外的标记,然后相应地定位:

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

【讨论】:

  • 我想要的是,IE 完全忽略 content2.tpl & Mozilla 或其他完全忽略 content1.tpl
【解决方案2】:

看看get_browser() PHP 函数。
http://php.net/manual/en/function.get-browser.php

请注意,USER AGENTS 是可以伪造的,所以你不能依赖这 100%。

【讨论】:

    【解决方案3】:

    在 PHP 中,您可以查看名为 $_SERVER 的超全局变量。在那里,在HTTP_USER_AGENT 键下,您会找到浏览器。

    这将在服务器端计算,使用 PHP 和 Smarty。

    在 Javascript 中,使用 this

    在 HTML 中你可以使用this syntax

    【讨论】:

      【解决方案4】:

      这项工作:

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

      查看HERE了解更多详情。

      【讨论】:

        【解决方案5】:
        <!--[if IE]>
        {include file="/content1.tpl"}       
        <![endif]-->
        
        <comment>
        {include file="/content2.tpl"}   
        </comment>
        

        Internet Explorer 会忽略 &lt;comment&gt;&lt;/comment&gt; 标记,就好像它们是标准 cmets,但所有其他浏览器都像普通代码一样读取它们。这非常适合隐藏非 IE 代码。

        【讨论】:

          【解决方案6】:

          这应该可行:

          <script type = " javascript" >
          if (navigator.appName == "Microsoft Internet Explorer")
          {
              /*
                do something
              */
          }
          else
          {
              /*
                do something else
              */
          }
          </script>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-22
            • 2013-08-10
            • 2018-11-16
            • 2016-10-29
            • 1970-01-01
            • 2017-11-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多