15.2. Document PropertiesHaving considered the legacy methods of the Document object, let's now turn to its legacy properties:
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. |