How can I determine the browser OS from Javascript?

I don't know anything about javascript, not programming, but I wanted to create a page that checked the os user, and if they use a mobile OS (iphone, android, etc.), forward them to the mobile site, and if they use computer, send them to a regular 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 he should do is check all the major computer OSs, and if it’s not one of them, send the user to a mobile web page, but if this is one of them, send them to the computer site.

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

Thanks Luke

+5
source share
7 answers

script?

, .

if, language, JavaScript-, .

Windows Mobile ( "Windows CE" ), Windows Phone, iPhone/iPad ( " Mac OS X" ) Android ( "Linux" ). , , - .

, , , . . , .

"" , , - . HTML-, .

+15

, , :

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

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

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

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

?

: , ( ) . CSS .

+7

, , :

- . JavaScript, , index.html - Apache , , , .

<html>
<head>
<meta http-equiv="refresh" content="0;document.location" />
</head>
<body>

<script language="javascript" type="text/javascript">// <![CDATA[
if (screen.width <= 699) {
document.location = "LINK TO YOUR MOBILE SITE";
}

else{ 
       document.location.href = "LINK TO YOUR DESKTOP SITE"
}
// ]]></script>
</body>

</html>

!

+6

, . :

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"
}
+5

This will show you how to use JavaScript to detect the browser, it is not so difficult. He will not actually say which OS, but since she will know if her browser on the phone or computer can be useful when switching to a mobile site, as you would like.

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

+3
source

All Articles