More on the document and window objects

We have already seen some of the document object's methods and properties in past examples. To fully utilise the power of JavaScript with frames-based HTML, we need to learn another property of the document object, and a little about the window object.

Nearly everything that comprises HTML documents has been converted into objects for use with JavaScript. The document object has the 'bgColor' and 'fgColor' properties that directly correspond to the foreground and background colours of the page. Another property of the document object is the location object, which has many properties itself. For now, the most relevant property is the 'href' property, which contains a string representation of the document's URL. This value can be accessed and examined in scripts, but, more importantly, it can also have new values assigned to it.

To display the HTML-encoded information in a Web document, a browser must first create a window to display the document. This window is created automatically by the browser, and is present every time you start up the browser. Unsurprisingly, JavaScript has a window object that pertains to the functions of a browser's display window. The document object is a property of the window object. The document object represents the HTML encoded page that is currently displayed in the browser's window. Because the document object is a property of the window object, every call to the writeln() method assumes this topmost reference to the Window object. Therefore, the statement

document.bgColor ="#000000";

is actually executing the statement

window.document.bgColor="#000000";