Section 15.2. Document Properties


15.2. Document Properties

Having considered the legacy methods of the Document object, let's now turn to its legacy properties:


bgColor

The background color of the document. This property corresponds to the (deprecated) bgcolor attribute of the <body> tag.


cookie

A special property that allows JavaScript programs to read and write HTTP cookies. This property is the topic of its very own chapter, Chapter 19.


domain

A property that allows mutually trusted web servers within the same Internet domain to collaboratively relax same-origin policy security restrictions on interactions between their web pages (see Section 13.8.2.).


lastModified

A string that contains the modification date of the document.


location

A deprecated synonym for the URL property.


referrer

The URL of the document containing the link, if any, that brought the browser to the current document.


title

The text between the <title> and <title> tags for this document.


URL

A string specifying the URL from which the document was loaded. The value of this property is the same as the location.href property of the Window object, except when a server redirect has occurred.

Several of these properties provide information about the document as a whole. You can place code like the following at the bottom of each of your web documents to provide a useful automated footer that allows users to judge how up-to-date (or out-of-date) a document is when it is printed:

 <hr><font size="1">   Document: <i><script>document.write(document.title);</script></i><br>   URL: <i><script>document.write(document.URL);</script></i><br>   Last Update: <i><script>document.write(document.lastModified);</script></i> </font> 

referrer is another interesting property: it contains the URL of the document from which the user linked to the current document. You can use this property to prevent deep-linking to your site. If you want all visitors to arrive via your own home page, you can redirect them by placing this code at the top of all pages except the home page:

 <script> // If linked from somewhere offsite, go to home page first if (document.referrer == "" || document.referrer.indexOf("mysite.com") == -1)     window.location = "http://home.mysite.com"; </script> 

Don't consider this trick to be any kind of serious security measure, of course. It obviously doesn't work for users who have disabled JavaScript in their web browsers.

One final Document property of interest is bgColor. This property corresponds to an HTML attribute whose use is deprecated, but it is listed here because of its history: changing the background color of a document was one of the first applications of client-side JavaScript. Even very, very old web browsers will change the background color of the document if you set document.bgColor to an HTML color string, such as "pink" or "#FFAAAA".

See HTMLDocument in Part IV for complete details on these legacy properties of the Document object.

The Document object has other important properties whose values are arrays of document objects. These document collections are the subject of the next section.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net