如何包含针对特定浏览器的 JavaScript?
包含 CSS 表很容易,并且与下面的代码配合得很好。然而,这一定不是包含 javascript 文件的好方法,因为当我在 iPad 中查看它时会收到错误。关于未封闭标签的一些事情。我想我通过这种方式获得了嵌套标签。我应该做什么呢?
<script type="text/javascript">
//Sniff out iPads
if (navigator.userAgent.indexOf('iPad') != -1) {
document.write('<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>
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.write('<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
ipad.js
,您可以在 javascript 中检测。对于CSS,只需使用条件注释(如果您支持较旧的 IE 版本)和media
属性 排除在 IE 和其他浏览器中加载它: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 themedia
attribute to exclude loading it in IE and other browers: