The JavaScript Diaries: Part 10

By Lee Underwood


This property returns a string listing the browser, the version number, operating system and, except for IE, the default language of the browser. For example (it would all be on one line, of course):

Mozilla/5.0 (Windows; U; Win98;
  en-US; rv:1.7.12) Gecko/20050915

You can use the following script to obtain information about all the navigator object properties. (The line in red should be combined to make one line):

var browser="Browser Information\n";
for (var propname in navigator) {
  browser += propname + ": "
    + navigator[propname] + "\n"

Go ahead and click the button and see what happens.


Before we analyze this script, let's look at a new operator. The in operator is used to inspect the contents of a specified object. The operand on the right of the in keyword (a string) references a property or method contained in the object on the left of the in keyword. For instance, to see if write is a property of the document object, you could use the following script (try it yourself and see what happens):

  var findIn="write" in document

Basically, the first statement above says, "look to see if the operand on the left of the in operator (write, in this case) is a property or method of the operand on the right of the in operator (document, in this case). If it is, return true; otherwise return false." Remember our discussion earlier about the object initializer. We used the example:

sysNeed = {monitor:"17\"",hd:"80GB",ram:"512MB"}

Using the in operator we could write the following, which would return true:

"monitor" in sysNeed

However, the following would be false. Do you see why?

"desk" in sysNeed

In the first statement, monitor is a string contained in the object sysNeed, so that would be true. In the second example, the string desk is not contained in the object sysNeed, so that would be false. Now, let's go back to our original script and see what's happening with each step. First, let's see the script again:

var browser="Browser Information\n";
for (var propname in navigator) {
  browser += propname + ": " + navigator[propname] + "\n"
  1. First, a variable named browser is declared and initialized with the value of the string, Browser Information:\n. (Remember, the \n tells the script to start a new line.)

  2. A for statement is then created. (The for statement is useful here because it allows for the declaration of the variable, the condition for the statement, and the incremental expression all in one.) In this statement we have declared the variable propname and used the in operator with the navigator object.

  3. We then said to add to the value of the variable browser the name of the string (propname) found in the navigator object, which we obtained in the previous line. Then, add a colon and display the properties of the navigator object. The square brackets ("[...]") are an operator used with associative arrays. These are used to dynamically associate arbitrary values with arbitrary strings. (Sounds impressive, doesn't it?) In this case, instead of listing each property separately, each one is displayed until there are no more to display. In a regular array, data would need to be declared in order for it to be used in the code, i.e.:
  4.   myArray = new Array(string1,string2,string3)

    Since we're using the navigator object, the data is already declared in the form of the properties contained within it. We'll look more at arrays in our next section.

  5. The results are then displayed in an alert window.

Page 2 of 5

Previous Page
1 2 3 4 5
Next Page

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date