Load mobile CSS if user is on Android

这一生的挚爱 提交于 2019-12-18 08:49:24

问题


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>

回答1:


You should just be able to add

|| (navigator.userAgent.match(/Android/i))

to the second if statement, so

if( (navigator.userAgent.match(/iPhone/i)) || 
    (navigator.userAgent.match(/iPod/i)) || 
    (navigator.userAgent.match(/Android/i)) ) 



回答2:


Here you goes :)

http://davidwalsh.name/detect-android




回答3:


This snippet will do it:

navigator.userAgent.match(/android/)


来源:https://stackoverflow.com/questions/6459692/load-mobile-css-if-user-is-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!