views:

154

answers:

6

I know nothing about javascript, and not a whole lot about programming, but I wanted to create a page which checked the user's os, and if they are using a mobile OS (iphone, android, etc...), forward them to a mobile website, and if they are using a computer, forward them to the normal website. Here is the page I made:

 <head> 
<title>OS Check | website.web.org</title> 

<SCRIPT LANGUAGE = "javaScript"> 

if (navigator.appVersion.indexOf("Win")!=-1) && (navigator.appVersion.indexOf("Mac")!=-1) && (navigator.appVersion.indexOf("X11")!=-1) && (navigator.appVersion.indexOf("Linux")!=-1) location.href= "http://website.web.org/mobile/index.html"

else location.href = "http://website.web.org/Home.html"

</SCRIPT> 
</head>

Basically, what its meant to be doing is checking all of the major computer OSs, and if its not one of them, sending the user to the mobile webpage, but if it is one of them, sending them to the Computer site.

Can someone please tell me what the error/problem in this page/script is?

Thanks, Luke

+2  A: 

The problem is that your parenthesis are wrong. Try this instead:

if ( navigator.appVersion.indexOf("Win")!=-1 
     && navigator.appVersion.indexOf("Mac")!=-1 
     && navigator.appVersion.indexOf("X11")!=-1 
     && navigator.appVersion.indexOf("Linux")!=-1 ){
       document.location.href= "http://website.web.org/mobile/index.html";
}
else{ 
       document.location.href = "http://website.web.org/Home.html"
}
klausbyskov
+10  A: 

What is wrong with this script?

Yeah, pretty much everything.

From the missing parentheses on the if statement, to the old-school language attribute, to the potentially-navigation-breaking JavaScript-redirect, to the whole idea of redirecting based on crude user-agent sniffing.

Your strategy will fail for Windows Mobile (contains “Windows CE”), Windows Phone, iPhone/iPad (contains the string “like Mac OS X”), and Android devices (contains “Linux”). That's a pretty good coverage of major mobile OSes to fail on, not to mention the desktop browsers that might not include any of those tokens.

You might be able to improve this by sniffing for particular cases you want to detect. See this list for an overview of what exists.

Treating all “mobile” devices as the same is unlikely to make sense when that category encompasses everything from barely-internet-capable featurephones to large-screen tablets. Modern mobile browsers are quite capable of rendering normal HTML pages, especially if you encourage accessibility by using liquid layout, and handheld-stylesheets to reduce necessary content width.

bobince
Not to mention that relatively a lot of mobile browsers have JS disabled.
BalusC
Indeed. At the very least, plain links should be included in the `<body>`. If you do have to go with a separate interface and sniffing (JS or otherwise), you will certainly need a manual method/link for the user to pick their interface manually, because any sniffing technique is going to fail a good proportion of the time.
bobince
...or both: `<a href="http://microsoft.com/" onclick="showFancyPopup(); return false;">Get IE!</a>`
Christian Sciberras
+2  A: 

Don't use these redirections client-side, I advise you to take of the scripts here: http://detectmobilebrowser.com/

netadictos
Yeah, while at it, start storing your password/user database here: [email protected]
Christian Sciberras
Sorry, what is all this about?
netadictos
+2  A: 

I would use the "browser feature detect", which detects if the browser supports certain features. If it does not then redirect them to a compatible version of the web site. It is safer in the long run because it will allow the best version of the website to be used when browsers are update to include new features.

https://developer.mozilla.org/en/Browser_Feature_Detection

Eric
+2  A: 

The other comments are all valid, but I think the root of your problem is this:

if (navigator.appVersion.indexOf("Win")!=-1) && (navigator.appVersion.indexOf("Mac")!=-1) && (navigator.appVersion.indexOf("X11")!=-1) && (navigator.appVersion.indexOf("Linux")!=-1)

when reformatted is

if (navigator.appVersion.indexOf("Win")!=-1)
    && (navigator.appVersion.indexOf("Mac")!=-1)
    && (navigator.appVersion.indexOf("X11")!=-1)
    && (navigator.appVersion.indexOf("Linux")!=-1)

which is equivalent to

if (isWindows
    && isMac
    && isX11
    && isLinux)

(because indexOf("foo")!=-1 means "found foo").

Can you see the logic error here?

That's why you should follow the advice of the other answers: format your code so it's readable, and (where possible) use an existing library. Sensible use of CSS is also your friend.

Cameron Skinner
+1  A: 

This will show you how to use JavaScript for Browser Detection, its not that hard. It won't actually tell which OS but since it will know if its a browser on a phone or computer it will be useful in directing to a mobile site like you wanted.

http://www.javascriptkit.com/javatutors/navigator.shtml

Blake