May 15, 2012

Is there a way to use <![if !(IE 8)]> within PHP?

Question by HudsonHawk

Sorry for the confusing title, I didn’t know how I could describe it better.

Anyways, here is the ‘problem’ I am currently using for my website Cufon. Which works great. However with Internet Explorer 8 it becomes really slow. Therefor I decided to use:

<![if !(IE 8)]>
<script type="text/javascript" src="js/myriad-pro.cufonfonts.js"></script>
<script type="text/javascript">Cufon.replace('.example1') ('.example2') ('.example3') ('.example4') ('.example5');
</script>
<![endif]>

This works great, however not for everything. Some parts of the scripts, which calls Cufon in the script itself, it doesn’t work and Cufon will still be displayed in IE8 (with all negative results from it).

Several included scripts / parts have the following code in it:

<?php
echo "<script language="javascript" type="text/javascript">";
echo "$(document).ready(function() { Cufon.replace('.orderpart1') ('.orderpart2') ('.orderpart3'); });";
echo "</script>";
?>

Now is it possible to make a same statement as with within the PHP script(s)? So if it’s any browser other than IE8 it will load the Cufon, but if it’s IE8 it will not load Cufon?

Hopefully someone understands what I mean here, cause it’s kinda hard to explain for me… 🙁

Answer by J-P

<?php
echo "<![if !(IE 8)]>";
echo "<script language="javascript" type="text/javascript">";
echo "$(document).ready(function() { Cufon.replace('.orderpart1') ('.orderpart2') ('.orderpart3'); });";
echo "</script>";
echo "<![endif]>";
?>

is that it ? Or did I misunderstand your request ?

Edit:
Another way, as I see that you are using jQuery, could be using jQuery browser detection :

<?php
echo "<script language="javascript" type="text/javascript">";
echo "if ( $.browser.msie && $.browser.version <= 8 ) {";
echo "$(document).ready(function() { Cufon.replace('.orderpart1') ('.orderpart2') ('.orderpart3'); });";
echo "}";
echo "</script>";
?>

Please note that this feature is deprecated :

This property is available immediately. It is therefore safe to use it to determine whether or not to call $(document).ready(). The $.browser property is deprecated in jQuery 1.3, and its functionality may be moved to a team-supported plugin in a future release of jQuery.

If you know exactly what kind of feature are needed and aren’t implemented in IE8 i would recommend using $.support which is meant for feature detection rather than browser detection.

Answer by Starx

Basically, you can do this by filtering guest’s USER AGENT[docs]. You can get this from the server variable.

$ua = $_SERVER['HTTP_USER_AGENT'];

USER agent contains all the necessary information of the guest who is opening the page. A simplest usage of this technique I know so far is:

if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT'])) {
    //do something
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!