Home

Wednesday 19 June 2013

Tutorial on how to detect browser platform and operating system

Simple JavaScript code to find on which Operating System is the user browsing your web page



The following script what I've written gets the information of the Operating System. This is achieved by writing JavaScript inside the HTML file & loading it via a Web Browser. What the browser does is that: It runs the HTML file and encounters a JavaScript within and then the Java Runtime Engine process it to get the Output.

<script language="JavaScript">

var platform = navigator.platform.substr(0,3);

</script>



What the above code does?



var platform | We're creating a variable called platform to hold the result that is returned by the script.

navigator.platform | navigator here is referred to as Browser & platform is referred to the Operating System on which the user is working.

substr() | It is a method which will return that the first three letters returned from all versions of Operating Systems.

(0,3) | Returns the (start,length) | With start being the position of the character you wish to start with and length being the number of characters that you want the method to evaluate.



How to print the value returned by the above code?



document.writeln("platform");



It will return Win if the user is on Windows

It will return Mac if the user is on Macintosh and so on...



As simple as that!!



No I'll show you the full working script to find the Platform where the user is on!



<head>

   <script language="JavaScript">

     var platform = navigator.platform.substr(0,3);

     document.writeln(platform);

   </script>

</head>



This code should be placed with the <head></head> tag as seen above.



If you didn't get it right, look below for the screen shot & verify if your script is written correctly or not.



JavaScript written in Dreamweaver:

 

World of Amazing Articles - JavaScript Platform detection
World of Amazing Articles - JavaScript Platform detection

 

Output as seen on Firefox Browser on Windows platform: 

 

World of Amazing Articles - JavaScript Platform Detection Output
World of Amazing Articles - JavaScript Platform Detection Output

Great!! Go ahead & apply this script in your Web Page & enjoy!

If you found this useful - please tell your friends!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments:

Post a Comment