If IE then include file [A] else include file [B]

回眸只為那壹抹淺笑 提交于 2019-12-11 08:14:54

问题


I have page for which I want to load different content for different browsers.

Example :

IF Internet explorer

{include file="/content1.tpl"}

ELSE if any other browser

   {include file="/content2.tpl"}

{/if}

Content1.tpl & Content 2.tpl are two different files having their respected Html & CSS.

How can I achieve this using Javascript OR php ?

Thanks,

  • Mandar

EDIT

What I want is, IE to completely neglect content2.tpl

& Mozilla or Other to completely neglect content1.tpl


回答1:


Don't use PHP for this. You are much safer doing this at browser level, preferably using conditional comments. You cannot trust the HTTP_USER_AGENT as it is very easy for forge/change so you cannot confidently (and therefore should not) make decisions based on its value. Stick to one .tpl file and either include a specific stylesheet based on a conditional comment or add additional markup using these comments. For example you can add additional markup like this and then target accordingly:

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



回答2:


Take a look at the get_browser() PHP function.
http://php.net/manual/en/function.get-browser.php

Note that USER AGENTS can be forged, so you can't rely on this 100%.




回答3:


in PHP you could look at the superglobal named $_SERVER. There, under the HTTP_USER_AGENT key you will find the browser.

That will be computed server-side, using PHP and Smarty.

In Javascript, use this

in HTML you could use this syntax




回答4:


This Work :

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

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

Look HERE for more details.




回答5:


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

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

Internet Explorer ignores the <comment></comment> tags as though they were standard comments, but all other browsers read them like normal code. This is great for hiding non-IE code.




回答6:


This should work:

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


来源:https://stackoverflow.com/questions/3141992/if-ie-then-include-file-a-else-include-file-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!