The JavaScript Diaries: Part 10/Page 2
[previous] [next]
The JavaScript Diaries: Part 10
userAgent
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 Firefox/1.0.7
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" } alert(browser);
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 alert(findIn);
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" } alert(browser);
- 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.) - A
for
statement is then created. (Thefor
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 variablepropname
and used thein
operator with thenavigator
object. - We then said to add to the value of the variable
browser
the name of the string (propname
) found in thenavigator
object, which we obtained in the previous line. Then, add a colon and display the properties of thenavigator
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.: - The results are then displayed in an alert window.
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.
[previous] [next]
Created: October 14, 2005
URL: