4

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>
Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
Ryan
  • 6,027
  • 16
  • 52
  • 89

3 Answers3

5

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)) ) 
Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
1

Here you goes :)

http://davidwalsh.name/detect-android

Xavier
  • 8,244
  • 18
  • 54
  • 85
0

This snippet will do it:

navigator.userAgent.match(/android/)
Joe
  • 80,724
  • 18
  • 127
  • 145