Skip to content Skip to sidebar Skip to footer

If IE Then Include File [A] Else Include File [B]

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 {includ

Solution 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>

Solution 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%.


Solution 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


Solution 4:

This Work :

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

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

Look HERE for more details.


Solution 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.


Post a Comment for "If IE Then Include File [A] Else Include File [B]"