Tuesday, January 31, 2012

Force IE8 to use IE7 Compatibility Mode

You might have heard about IE8 issues that can be solved by putting the browser into IE7 Compatibility Mode. While this can be done by the visitor itself, it of course makes much more sense if your website is able to tell this to the website itself.

Setting the X-UA-Compatible tag


The trick is simple: If you add the following tag to your Joomla! template, it will tell IE8 to use the IE7 Compatibilty Mode instead.


<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />


However, this tag appears for all browsers, and not just for IE8. To prevent this, we need to add a browser-check on PHP-level. The Joomla! Framework contains some good classes to allow for this, but to get to the point quickly, we'll use our free Yireo Template Helper class Yth:

require_once 'yth.php';

With Yth you can quickly check if the current browser is IE8 and act accordingly:


<?php if (Yth::isBrowser('ie8')) { ?><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /><?php } ?>

Sending HTTP headers


Unfortunately this doesn't work (always). Sometimes the IE8 browser doesn't pick up the META-tag, and to fix that we actually need to send the same tag as HTTP header. We can still do this within the Joomla! template - however, we need to make sure to include these PHP-lines before the first HTML-tag appears (before the DOCTYPE-type for instance).

require_once 'yth.php';

if (Yth::isBrowser('ie8')) header("X-UA-Compatible: IE=EmulateIE7");
 
 Source: Collect from internet 
 
Developer and Project Manager: Ashik Mahmud

Odesk Hire Link: 

 

No comments: