Link to home
Start Free TrialLog in
Avatar of cyberspy
cyberspy

asked on

Need code for detecting OS?

Hi everyone,

I am wondering if there's any code for detecting an operating system that the user is using?  Let's say if a user goes to a specific webpage I want it to detect his OS and then depending if it's Windows or Mac to redirect him to another appropriate page. Is that possible? If yes, please help me out with the script.  I really appreciate it.

--
cyberspy.
Avatar of b1xml2
b1xml2
Flag of Australia image

<script language="javascript">

//general redirection
function redirectPage(szWinURL,szMacURL) {
 if (navigator.userAgent.match(/Mac/i)) {
   if (szMacURL.length != 0) window.location.replace(szMacURL);
 } else {
   if (szWinURL.length != 0) window.location.replace(szWinURL);
 }

}

//e.g. of redirecting if Macs are found
redirectPage("","mac.html");
//e.g. of redirecting if window is found (default)
redirectPage("win.html","");
//e.g. redirecting
redirectPage("index_win.html","index_mac.html");

</script>
Avatar of cyberspy
cyberspy

ASKER

Thanks for your reply.  Only one question, what is the difference between mac.html and index_mac.html?
You explained in e.g. parts of redirecting for Mac and windows and then just redirecting? please explain me the last one.  Thanks.
the last one guarantees a redirection, either to the page for Macs or Windows. The function provided is flexible enuff for that.
Ok, I just tried it and it didn't work.  I place this code in my index.html page and specified index.html for windows redirect and mindex.html for mac redirect and it didn't work.  I'm not sure why...?
the code Brandon provided was client side, i can offer a server side function if you support ASP?

BRUNO
No, client side is fine, but it didn't work when I opened the page on Mac, it stayed on the index.html page.  How can I get it to work? Please help me out.
ASKER CERTIFIED SOLUTION
Avatar of bruno
bruno
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked just fine, Thanks BRUNO:)!!
Glad I could help!  Thanks for the A.  :-)