views:

39

answers:

3

Here is my code:

<script type="text/javascript"> 
if (navigator.userAgent.indexOf('BlackBerry')!= -1){
    document.write('<link rel="stylesheet" href="/m/css/mobile.css" type="text/css" />');
} else if (navigator.userAgent.indexOf('iPhone')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
}else if (navigator.userAgent.indexOf('Android')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
} else if (navigator.userAgent.indexOf('Windows XP')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
}
else{
    document.write('<link rel="stylesheet" href="/m/css/mobile.css" type="text/css" />');
}
</script> 

Clearly my Windows XP one is wrong. Anyone know what I need to replace it with so that the /css/mobile.css only shows up on phones other than the iPhone and Android devices.

Whether I need to add Linux and Windows or all the popular browsers instead of OS

+2  A: 

you need a user-agent for a browser, not an operating system, but here you go: 'Windows NT' for windows

and: 'MSIE' explorer 'Gecko' for firefox (and browsers with gecko engine)

but seriously, you shouldn't need to know what os it is, but what browser

fazo
Bry4n
'Chrome' for chrome and 'Opera' for opera
fazo
Gecko will get you Google Chrome - even though Chrome DOESN'T have the gecko engine - this is the reason UA sniffing is always a BAD idea. Just don't do it, there's always far better options no matter what you're developing.
lucideer
I personally would use server-side code but I am restricted.
Bry4n
+1  A: 

Maybe this link can help you: Browser Detection

But filter by OS is not a good choice. You must make this filter by browser because an OS can have all the browsers, and the website should look the same on all the browsers no matter what OS is being used.

The script from this link covers the recent browsers being used today, so it should fit what you want. =)

Fabiano
+1  A: 

I already answered this exact question when you asked it here: http://stackoverflow.com/questions/3268740/desktop-os-user-agents-in-javascript

lucideer
I was looking for it cause I thought I asked it already. I apologize.
Bry4n
@Bry4n: You can access a list of your questions on your user page, you know...
Matti Virkkunen
@bry4n Check your user profile, it lists all the questions you've asked/answered, plus a bunch of other things. http://stackoverflow.com/users/316429/bry4n
George Marian
Yes I do know. I just didn't see it.
Bry4n