The document object is created by the browser for each new HTML page that is viewed. We have already seen write() method of the document object which writes a text into an HTML document. The document object works based on the Document Object Model (DOM). Each HTML document is treated as a structure of nodes. The scripting language can access elements within the document.
Properties of Document Object
The title property holds the string value of the title of the HTML document. For example:
The URL property holds the value of the complete URL of the current document. For example:
The URLUnencoded property returns the URL of the document without any encoding. For example:
The referrer property is used for informational purpose and holds the value of the URL of the page the user came from. For example:
The lastModified Property holds the value of the date and time the current document was last modified. For example:
The dir property returns a string value that represents the reading direction of the document. The value can be either ltr (left to right) or rtl (right to left). For example:
document.dir = “rtl”;
The cookie property is used to store users information. Cookie is a small text file stored on users computer for a specified amount of time. For example:
document.cookie = string;
Methods of Document Object
write() and writeln() Method
The document.write() method is used to display string value on the page where it is called.
The document.writeln() methods works the same way, but adds a newline character at the end of the statement.
getElementById() Method: The getElementById() method allows the access to an element by the value of its id attribute.
getElementsByName() method: This method allows to access an array with all the elements in the document that have the specified value for the name attribute.
getElemntsByClassName() method: This method allows you to get an array filled with all the elements in the document that have specified class name.
getElementsByTagName() Method: This method allows to get an array with all the elements in the document that have the specified tag name.
open() and close() methods
The open() method allows to open a new document and create its content entirely with document.write() statements. When open() method is used the browser checks for the statements to write the new page. The close() method is written after completion of all the write() statements to finish the new page.