Another Location—document.location
(document.URL)
So far, any reference to the location object defaulted to window .location.
But, there is another location in JavaScript—document .location. To
avoid confusion, Netscape decided to change document.location to document.URL.
Although Navigator 3.0 supports both references, Netscape has announced that the document.location
property reference will not be supported by future releases. Microsoft’s Internet
Explorer 3.0 supports only document.location. In this chapter, we are following
Netscape’s decision to use document.URL.
The property document.URL holds the complete URL of the current document. In
contrast to window.location(.href), it is a read-only value. It does not know
windows from frames but just the document that contains the script and the reference to
this property. Keep in mind that the URL belongs to the document, not to the window.
Therefore, when a window consists of multiple documents including frame structures, a
single frame’s document.URL is different from window.location(.href),
from any other frame’s document.URL, and from the main frame set’s document.URL.
If you want to load a new document into the browser’s window, you have to use the
write-enabled window.location (==window.location.href). If you want the URL of a
specific frame, you should use document.URL. You can also use this property to
retrieve the URL of the window if the document does not have frames and you are sure you
do not want to change the URL (to load another page).
Since document.location is still in use (especially with MSIE 3.0), you must
be very careful when using the location property. If you are not a very
experienced scripter or do not fully understand the object scoping in JavaScript, it is a
good practice to always specify the calling object, window or document.
When you refer to location in a script, it defaults to window.location,
because window is the default object inside scripts. However, when you specify
the bare location in an event handler script, the calling object defaults to document;
that is, location defaults to document.location.
Important rule: Although the href property is the default one, always use
location.href to refer to the URL of the window. If you follow this rule, trying to
specify location.href for a document will generate an easy-to-fix error. If you just use
location rather than location.href, it will be accepted by both windows and documents and
you will have a hard time debugging the problem. |
Summary
In this chapter we discussed JavaScript’s URLs. At first, we introduced common URL
terms, including the various supported schemes (http, ftp, gopher,
etc.). We then presented the window.location object with all its properties and
methods. The href property is used to link documents to each other. We have also
discussed a URL-processing-based multiple-engine search utility. We have introduced
another URL-related element, the document.URL property, also known as document.location.
You should be mastering URL handling by now, because we will move on to more advanced
URL-related concepts, such as frames, later in the book.