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>
</font>
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:
<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. One obvious flaw is that it doesn't work
for browsers that don't support JavaScript or for users who
have disabled JavaScript.
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.