开发者

Conditionally load jQuery

开发者 https://www.devze.com 2023-02-21 07:14 出处:网络
I have a website which is loading jQuery from Google\'s CDN servers: <script type=\"text/javascript\"

I have a website which is loading jQuery from Google's CDN servers:

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

For those browsers with javascript disabled, the website still functions well. (Graceful degradation).

For users with IE6, the javascript functions, but it doesn't look pretty. What I am looking to do is to prevent jquery from loading on IE6.

I thought the following would work:

<!--[if gt IE 6]>
<script type="text/javascript"
src="http://ajax.goog开发者_运维问答leapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<![endif]-->

Conditional tags - if browser is greater then IE6, load jQuery. However, I believe that prevents JQuery from loading in Safari, Chrome, Firefox, etc.

Another solution is to detect the browser in PHP and then load jQuery for browsers other then IE6. Is that the best option??


You need to use downlevel-revealed comments:

<![if gt IE 6]>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<![endif]>


Go with the conditional comments, as you've got it, only without the -- characters (so that other browsers don't treat it as a comment).

Trying to do browser detection on the server is always going to be asking for trouble. It's just not 100% reliable because it can be spoofed. The advantage of the conditional comments is that it cannot be spoofed - it will always work.

Personally, I'd just ignore IE6 - it's rapidly disappearing from the browser market share (down to below 2% in most of the developed world now), and having a site look ugly in IE6 can only help accelerate it's demise. But well done for writing a site that degrades gracefully and can cope with IE6. Kudos for that, because it's more effort than most people go to these days.

0

精彩评论

暂无评论...
验证码 换一张
取 消