简体   繁体   中英

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

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... :(

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

Yes you can use the HTTP_USER_AGENT

$using_ie8 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.') !== FALSE);

but we aware that this can be changed by the user.

Here is a list of all User Agents for IE

In PHP, you can use get_browser() to detect the browser and its version.

$browser = get_browser(null, true);

This should return you an array containing browser information.

Note that this will require browscap setting.

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
}

Just print the conditional comments as well;

<?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]>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM