04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: The Document Object Model<br />

❑<br />

❑<br />

IE, Safari 3.1 and later, Opera, and Chrome create a comment node for the first comment but not<br />

for the second. The first comment becomes the first node in document.childNodes .<br />

Firefox as well as Safari prior to version 3.1 ignore both comments.<br />

Once again, the inconsistent behavior makes accessing comments outside the < html > element essentially<br />

useless.<br />

For the most part, the appendChild() , removeChild() , and replaceChild() methods aren ’ t used on<br />

document , since the document type (if present) is read - only and there can be only one element child<br />

node (which is already present).<br />

Document Information<br />

The document object, as an instance of HTMLDocument , has several additional properties that standard<br />

Document objects do not have. These properties provide information about the web page that is loaded.<br />

The first such property is title , which contains the text in the < title > element and is displayed in the<br />

title bar or tab of the browser window. This property can be used to retrieve the current page title as well<br />

as to change the page title such that the changes are reflected in the browser title bar. Changing the value<br />

of the title property does not change the < title > element at all. Here is an example:<br />

//get the document title<br />

var originalTitle = document.title;<br />

//set the document title<br />

document.title = “New page title”;<br />

The next three properties are all related to the request for the web page: URL , domain , and referrer . The<br />

URL property contains the complete URL of the page (the URL in the address bar), the domain property<br />

contains just the domain name of the page, and the referrer property gives the URL of the page that<br />

linked to this page. The referrer property may be an empty string if there is no referrer to the page. All<br />

of this information is available in the HTTP header of the request and is simply made available in<br />

JavaScript via these properties, as shown in the following example:<br />

//get the complete URL<br />

var url = document.URL;<br />

//get the domain<br />

var domain = document.domain;<br />

//get the referrer<br />

var referrer = document.referrer;<br />

The URL and domain properties are related. For example, if document.URL is http://www.wrox.com/<br />

WileyCDA/ , then document.domain will be www.wrox.com .<br />

271

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!