Jquery / javascript to detect an OS without a plugin?

I am looking for a way to detect the OS for a download page using jQuery or Javascript to recommend specific files for Mac and Windows. I was hoping to do this without adding another plugin to my page.

+36
javascript jquery operating-system
Aug 12 '11 at 18:52
source share
7 answers

Try:

var os = navigator.platform;

Then process the os variable accordingly for your result.

You can also scroll through each navigator object object to help you become more familiar with the objects:

 <script type="text/javascript"> for(var i in navigator){ console.log(i+"="+navigator[i]+'<br>'); } </script> 
+55
Aug 12 '11 at 18:55
source share

Normal JavaScript may be all you need.

 var OSName="Unknown OS"; if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; document.write('Your OS: '+OSName); 

As Nick suggested using navigator.platform .

+31
Aug 12 '11 at 18:54
source share

As far as I know, platform is a less fake property in the navigator object. You can use this to get logical data.

 var isMac = navigator.platform.toUpperCase().indexOf('MAC')!==-1; var isWindows = navigator.platform.toUpperCase().indexOf('WIN')!==-1; var isLinux = navigator.platform.toUpperCase().indexOf('LINUX')!==-1; 

If you need to distinguish the Mac from the old PowerPc and the new Intel.

 var isMacPpc=navigator.platform==="MacPPC"; var isMacIntel=navigator.platform==="MacIntel"; 

https://developer.mozilla.org/en/DOM/window.navigator.platform

+13
Aug 01 2018-12-12T00:
source share

Try:

 alert(navigator.appVersion); 

This should give you a line that you can parse for the OS.

+6
Aug 12 '11 at 18:55
source share
 <script> osName = 'Unknown'; function nav(x, y, z) { z = z || y; if (navigator[x] && navigator[x].indexOf(y) !== -1) { osName = z; } } /* navigator value download */ nav( "appVersion", "X11", "UNIX" ); nav( "appVersion", "Mac", "MacOS" ); nav( "appVersion", "Linux" ); nav( "userAgent", "Linux" ); nav( "platform", "Linux" ); nav( "appVersion", "Win", "Windows" ); nav( "userAgent", "Windows" ); nav( "platform", "Win", "Windows" ); nav( "oscpu", "Windows" ); document.getElementById("download"+osName).className = "knownOS"; </script> 

Make sure that the correct download link is easy to find, but does not hide other links to the OS. People may still want them.

 <style> #downloadUNIX, #downloadMacOS, #downloadLinux, #downloadWindows { color:#6D94F2; line-height:35px; margin:24px 0 24px 0; padding:10px; } .knownOS { background-color:#F7ECAD !important; border:2px solid #E8913A; color:#133CC4 !important; font-weight:bold; } </style> 

And some html

 <ul> <li><a id="downloadUNIX" href="unix Link Here" >Download Napster-9000 for UNIX</a></li> <li><a id="downloadWindows" href="windows Link Here">Download Napster-9000 for Windows</a></li> <li><a id="downloadMacOS" href="mac os link here" >Download Napster-9000 for OS X</a></li> <li><a id="downloadLinux" href="linux Link Here" >Download Napster-9000 for Linux</a></li> </ul> 

Now the user can disable or block javascripts if he wants. Links will still be present, not writing links with Javascript, which requires javascript to work.

Here is the fiddle

http://jsfiddle.net/7fmJb/

+5
Jan 06 '14 at 7:37
source share

You can use this function inside the document ready function. Add the code inside the function for your event.

 function checkOperatingSystem() { var userAgent = navigator.userAgent || navigator.vendor || window.opera; //Check mobile device is Android if (/android/i.test(userAgent)) { //Add your Code here } //Check mobile device is IOS if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) { //Add your Code here } //Check device os is Windows (For Laptop and PC) if (navigator.appVersion.indexOf("Win")!=-1) { //Add your Code here } //Check device os is MAC (For Laptop and PC) if (navigator.appVersion.indexOf("Mac")!=-1) { //Add your Code here } } 
0
Sep 17 '18 at 6:40
source share

I think this may help:

 navigator.appVersion 

Try to console this in your JS file. For example:

 console.log(navigator.appVersion); 

You will get most of the information regarding the OS.

0
Dec 22 '18 at 10:59
source share



All Articles