views:

30

answers:

1

Below is my current script:

<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>

The Windows XP is not working, and it's defaulting to the last else clause on a desktop computer. I need either the OS IDs or the browser's IDs (i .e firefox, chrome, IE, opera).

+1  A: 

Your "Windows XP" line is not working as Windows XP identifies itself as "Windows NT 5.1".

However, I'm curious as to why you'd want to do this at all. UserAgent sniffing in any form is generally frowned upon in the web-development community - there's always a far better (usually far easier) way.

lucideer
It's for our mobile website. I want a certain CSS to show for Blackberrys and another for the smarter phones. I can not use any server side coding sadly.
Bry4n
Why is it frowned upon? I think its fairly common, so you can direct users to a version of your site which supports their environment.
Nate Bross
@Bry4n you can use CSS media queries to do that without browser-sniffing. See: http://vimeo.com/12976832 http://www.alistapart.com/articles/pocket/
lucideer
@Nate It's frowned upon for a large number of reasons - mostly because it's a maintenance nightmare requiring you to constantly maintain and update a list of browser support, but also because the alternative methods are all easier and cleaner and more reliable. See: http://css-tricks.com/browser-detection-is-bad/ http://www.sitepoint.com/blogs/2009/05/31/why-browser-sniffing-stinks/
lucideer
@Nate: Internet Explorer is fairly common, yet frowned upon by everyone.
Matti Virkkunen