如果用户使用 Android,则加载移动 CSS
我的 .htaccess 文件正在写入一个“智能”cookie。如果我的页面读取此 cookie,它将写入一个 div。然后,仅当用户使用 iPhone 或 iPod 时,我的移动 CSS 文件才会加载。
我的问题是,如果用户使用的是 Android,我该如何编辑此代码(如下)来加载移动 CSS 文件?
这是我的页面和代码:
<meta name="viewport" content="width=device-width, user-scalable=yes" />
<link rel="stylesheet" type="text/css" href="css/mobile.css" media="only screen and (max-width: 640px)" />
<script type="text/javascript">
if (window.screen.width > 640){document.write('<meta name="viewport" content="width=980, user-scalable=yes" />')}
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))){document.write('<link rel="stylesheet" type="text/css" href="css/mobile.css" media="only screen and (max-width: 640px)" />')}
</script>
My .htaccess file is writing a "smart" cookie. If my page reads this cookie it will write a div. Then my mobile CSS file loads only if the user's on an iPhone or iPod.
My question is, how can I edit this code (below) to load the mobile CSS file if the user's on an Android?
Here's my page and code:
<meta name="viewport" content="width=device-width, user-scalable=yes" />
<link rel="stylesheet" type="text/css" href="css/mobile.css" media="only screen and (max-width: 640px)" />
<script type="text/javascript">
if (window.screen.width > 640){document.write('<meta name="viewport" content="width=980, user-scalable=yes" />')}
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))){document.write('<link rel="stylesheet" type="text/css" href="css/mobile.css" media="only screen and (max-width: 640px)" />')}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够添加
|| (navigator.userAgent.match(/Android/i))
到第二个
if 语句
,所以You should just be able to add
|| (navigator.userAgent.match(/Android/i))
to the second
if statement
, so给你:)
http://davidwalsh.name/detect-android
Here you goes :)
http://davidwalsh.name/detect-android
这个片段可以做到这一点:
This snippet will do it: