如何包含针对特定浏览器的 JavaScript?

发布于 2024-10-01 01:47:39 字数 481 浏览 1 评论 0原文

包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2024-10-08 01:47:39

对于ipad.js,您可以在 javascript 中检测。对于CSS,只需使用条件注释(如果您支持较旧的 IE 版本)和 media 属性 排除在 IE 和其他浏览器中加载它:

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

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]-->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文