home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

JavaScript: The Definitive GuideJavaScript: The Definitive GuideSearch this book

14.4. Document Information Properties

Several properties of the Document object provide information about the document as a whole. For example, the following code shows how you can use the lastModified, title, and URL properties to include an automatic timestamp within a document. This feature allows users to judge how up-to-date (or out-of-date) a document is, and it can also be useful information when a document 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>

referrer is another interesting property: it contains the URL of the document from which the user linked to the current document. One possible use is to save this value in a hidden field of a form on your web page. When the user submits the form (for whatever reason your page contains the form in the first place), you can save the referrer data on the server so you can analyze the links that refer to your page and track the percentage of hits that come through various links. Another use of this property is a trick to prevent unauthorized links to your page from working correctly. For example, suppose you want to allow other sites to link only to the top-level page on your site. You can use the referrer property in conjunction with the location property of the Window object to redirect any links from outside the site to the top-level home page:

// 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";

Don't consider this trick to be any kind of serious security measure, of course. One obvious flaw is that it doesn't work for browsers that don't support JavaScript or for users who have disabled JavaScript.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.