开发者

How to include JavaScript for a specific browser?

Including a css sheet is easy and works well with the code below. However this must not be a good way to include a javascript file because I get an error when I view it in the iPad. Something about an unclosed tag. I assume I'm getting nested tags by doing it this way. What should I be doing instead?

<script type="text/javascript">
//Sniff out iPads
if (navigator.userAgent.indexOf('iPad') != -1) {
    document.wri开发者_如何学运维te('<link rel="stylesheet" type="text/css" href="/styles/browser/ipad.css" media="screen, print" />');
    document.write('<script src="/scripts/ipad.js" type="text/javascript" />');
}
<script>


For the ipad.js you can detect inside the javascript. For the CSS just use conditional comments (if you're supporting older IE versions) and the media attribute to exclude loading it in IE and other browers:

<!--[if !IE]>-->
<link rel="stylesheet" type="text/css" href="/styles/browser/ipad.css" media="only screen and (max-device-width: 480px)" />
<![endif]-->
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜