Document Object


JavaScript provides a number of application objects such as the document, window, string, and Date objects. Each object has properties that can be read and sometimes set in JavaScript statements. Many objects have methods such as document.write() that give the object significant functionality. Information about the object often called the attributes of the object are stored in variables called "properties" of the object. Some of the basic properties and methods of the document object are introduced below using simple JavaScript statements.

Properties

Properties are the attributes of an object. For example the background color and the URL the document was retrieved from are attributes of an HTML document. In the examples below various properties of the document object are accessed using the dot operator and assigned to a variable called result.

Getting the Properties of an Object

result = document.location
result will contain the URL of the document.
 
result = document.lastModified
result will contain the date of the last time the document was modified.
 
result = document.bgColor
result will contain the background colour of the document.

Setting the Properties of an Object

It is also possible to assign values to some object properties. So while it does not make sense to be able to change the location where the document came from, you can change the background color of the document this way:
document.bgColor = "#800080"

Methods

Methods are a way of asking an object to do something more complicated than changing a property. For example:

document.write( JavaScript expression )
write() is a method (a function) that belongs to the document object. Calling write() is like asking a document to append HTML to itself. The JavaScript expression inside the round brackets will be evaluated and the result (whatever it is) written out to the HTML page. Here are some more methods for the document object:
document.writeln( JavaScript expression )
writeln() does the same thing as the write() method except that it appends a new line character to the string of text it writes into the document. writeln is a convenient method to use when writing out data from short test programs within <PRE></PRE> tags. Here are some more methods for the document object:
document.clear()   
Clears the document (doesn't appear to do anything in Netscape 2.02)
document.close() 
"Closes the output stream and forces data sent to layout to display." In Netscape 2.02 it is not required to display the last output.
document.open()
"Opens a stream to collect the output of write or writeln methods."
Flag Counter