最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

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

Share Improve this question asked May 15, 2012 at 9:09 JoanneJoanne 5143 gold badges9 silver badges30 bronze badges 2
  • You'll want PHP to detect the USER_AGENT, and send the Cufon-related scripts only when the version is not IE8. Try an existing PHP browser-version detection script - rolling your own is guaranteed to not catch that one exception... – Konerak Commented May 15, 2012 at 9:13
  • Server-side is not the browser. Just echo all the resources and use feature detection to find IE and run it. – Joseph Commented May 15, 2012 at 9:26
Add a ment  | 

5 Answers 5

Reset to default 5
<?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 remend 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.

Just print the conditional ments 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]>";
?>

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
}
发布评论

评论列表(0)

  1. 暂无评论