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 completel开发者_开发问答y neglect content2.tpl
& Mozilla or Other to completely neglect content1.tpl
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>
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%.
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
This Work :
<!--[if IE]>
{include file="/content1.tpl"}
<![endif]-->
<![if !IE]>
{include file="/content2.tpl"}
<![endif]>
Look HERE for more details.
<!--[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.
This should work:
<script type = " javascript" >
if (navigator.appName == "Microsoft Internet Explorer")
{
/*
do something
*/
}
else
{
/*
do something else
*/
}
</script>
精彩评论