JavaScript: Properties

 

Browser Properties

The browser you are using (probably Internet Explorer or Netscape) has several properties, and with JavaScript you can find out those properties. The name of the browser object is 'navigator.' You can use these properties to find out which browser a viewer of your page is using. This might be useful if, for example, you wanted the viewer to see one page if they were using Internet Explorer and a different page if they were using Netscape (more on this later).

Here are some of the key browser properties:

Browser Property Description
appName name of the browser
appVersion version of the browser and the platform it is created for
appCodeName code name of the browser
userAgent hypertext transfer protocol header used by the browser in dealing with servers

Let's write a script that will find out these properties and print them to the screen. Here's the script:

<BODY>

<script language=JavaScript>

<!--

name = navigator.appName

document.write("<h6>The browser you are using is " + name)

version = navigator.appVersion

document.write("<br><h6>The browser version is " + version)

code = navigator.appCodeName

document.write("<br><h6>The browser code name is " + code)

agent = navigator.userAgent

document.write("<br><h6>The browser user agent is " + agent + "</h6>")

//-->

</script>

to see this how this code looks in a new window.

 

Document Properties

The 'document' object of a web page has many properties as well. Following is a summary of some of the properties of the 'document' object:

Document Property Description
document.bgColor background color
document.fgColor foreground color
document.linkcolor color of links
document.alinkcolor color of active links
document.vlinkcolor color of visited links
document.URL URL of page
document.referrer page that preceded the current page
document.title title of page
document.lastModified date that page was last changed

 

Other Resources

Try these links on browser and document properties:


Page Link
Concept of Properties www.htmlgoodies.com/primers/jsp/article.php/3478481
JavaScript Properties www.htmlgoodies.com/beyond/javascript/js-ref/article.php/3471031

or check out one of our reference books, or look for other help on the Web.

 

Exercise

This exercise (for once) has nothing to do with the eEverything.com website. Write a script that writes to the screen the value of the 9 properties listed above under "Document Properties." This script will probably be similar to the script above which writes to the screen 4 key properties of the browser.