This tutorial will walk you through what information can be accessed as well as how to display that information. Additionally, we'll discover some basics about what information modern web browsers are able to obtain about your computer, it's OS, what browser you use, your location, and more.
First we need to know what information is accessible. Refer to the list below for a sampling of what data is accessible by web browsers. This is not an exhaustive list:
*source: JavaScript: The Web Warrior Series: By Sasha Vodnik and Don Gosselin
Using the list above, let's begin wriring our script to display the properties listed. We're going to start with appVersion.
We're going to use the Navigator Object to gain and display information from our web browser.
We will use getElementsByTagName to display our content. You must format your html with "p" tags in order to accomplish this. This page has six "p" tags, which is why the number 5 follows the command "document.getElementByTagName("p")". Remember, in JavaScript, numbering begins at zero, not one. So, we want to display our data on the sixth "p" tag, which means we'll use the number [5]. The number five represents the sixth "p" tag in our html document.
See the code below:
document.getElementsByTagName("p")[5].innerHTML =
"<strong>Web browser version: </strong>" + navigator.appVersion;
Entering the above code will display the following information:
To view the other items in the list, simply replace what we referenced here, "appVersion", with the other items in the list. For example, the code below will display the operating system, or "platform", installed on your computer:
document.getElementsByTagName("p")[8].innerHTML =
"<strong>Operating platform: </strong>" + navigator.platform;
The results are displayed below:
You can also access a simplified demo of just the JavaScript code wiuth just two "p" tags in the html on jsfiddle.
You can continue in this manner to display all the information referenced. GeoLocation can be used in a variety of ways to display information. For more info, visit the w3c School's HTML Geolocation API information page.