Deconstructing the Scripts

On the previous page, I pasted a series of scripts that utilize properties of four specified objects: document, navigator, history and location. The first line of the first script reads:

document.write("The background color of this page is <B>"
+document.bgColor+ "</B>.")

What do the Plus Signs do?

The plus signs on either side of document.bgColor are crucial to getting the results of the script. Whatever is within double quotation marks will be written to the page, as with any document.write statement. The plus signs set aside the object.property code as something that should be returned. The text document.bgColor should not appear on the page, because only the property, which that line of text represents, should be displayed, and that is the document's background color. When statements are enclosed inside plus signs, JavaScript must find the property's value and replace the statement with that value and write that to the HTML page. The browser will display:

Why the Property Value displayed in Bold?

That is caused by the <B> and </B> statements, placed inside double quotes, on either side of the object.property code. Being a document.write statement, the bold commands are written to the page and act upon the property that was returned. Web designers can affect what is returned from the script, rather than just the text written within the document.write statement. Those HTML commands must be inside double quotes so that they are seen as text, rather than part of the script commands. If they are not quoted, then an error will result!

Back Table of Contents Next