The document object stores the elements of an HTML document, such as HTML, HEAD, BODY, and other HTML tags as objects.
A document object is a child object of the Window object, which refers to the browser.
You can access a document object either using
window.document property or using object directly.
See, What we can do with Document Object:
Suppose you have created a FORM in an HTML document using the FORM element and added some text fields and Buttons on the Form. On Submitting the Form you want to validate the input or display input on another page. In this situation, you can use a document object which is a child object of the window object. Using the document object, you can access the elements of the form and validate the Input.
The Document Object provides different collection elements, such as anchor and Links which helps you to count the number of specific elements on a form.
The Document Object also provides various properties such as
bgcolor, etc. which will allow you to retrieve the details of a document and various methods such as
getElementByName(), etc. which allow you to perform various tasks like opening an HTML document to display output and writing a text on Web Page.
The Document Object also allows you to create cookies for a webpage by providing a property named cookie. A cookie stores information about the user's computer, which helps in accessing visited websites.
Now let's explore document object methods and properties.
As we know, a property of an object is the value associated with the object. The property is accessed by using the following notation:
where objectName is the name of the object and propertyName is the name of its property.
Now we will show you the properties of document object in the table given below:
|cookie||returns a report that contains all the visible and unexpired cookies associated with the document|
|domain||returns the domain name of the server from which the document has originated|
|lastModified||returns the date on which document was last modified|
|documentMode||returns the mode used by the browser to process the document|
|readyState||returns the loading status of the document.|
|referrer||returns the URL of the documents referred to in an HTML document|
|title||returns the name of the HTML document defined between the starting and ending tags of the TITLE element|
|URL||returns the full URL of the HTML document.|
Let's take an example, to see the document object properties in action:
Now we will show you some of the commonly used methods of the document object:
||opens an HTML document to display the output||
||closes an HTML document||
||returns the reference of first element of an HTML document with the specified id.||
||returns the reference of an element with the specified name||
||returns all elements with the specified tagname||
Now let's take an example, to see the document object methods like
getElementById() to explain their usage. See the below example: