Javascript DOM: Document Object

The World Wide Web Consortium created and released the (DOM) Document Object Model, which is a web interface. This organization was established to set the standards for the World Wide Web.

The DOM is a language-independent web API. This implies that it may be implemented and used in any programming language.

The DOM objects represent the structural elements of web content that can be accessed and edited. In other terms, the DOM enables you to achieve the following as a software developer:

  1. Construct and Create web documents.
  2. Navigate web document structures.
  3. In web documents, you may add, change, or remove parts and information.

Why is the Document Object Model (DOM) Useful?

HTML is used to organize web pages, whereas Javascript is useful for adding functionality to web pages. When HTML files are loaded into a browser, javascript cannot directly interpret the HTML content. As a result, a relevant document is produced (DOM).

DOM is essentially a depiction of the same HTML content in a new manner using objects. Javascript readily interprets DOM, for example, javascript can't comprehend tags (h1>H/h1>) in an HTML doc. but it can understand h1 in DOM.

Javascript can now use various functions to access each of the objects (p, h1, etc.).

Document Object Model Structure

DOM can be compared to a forest or tree. The phrase - structural model, is occasionally used to characterize a document's tree-like structure. Each of the tree's branches terminates in a node, so each node includes objects. Event listeners may be attached to nodes and activated when a specified event occurs.

The structural isomorphism of DOM structure models is an essential property: if two DOM implementations are used to construct a depiction of the same document, they will produce the very same structure model with exactly the same objects.

Here's the HTML doc. for the DOM tree shown above:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>This is the Title</title>
    <link rel="stylesheet" href="style.css">
        <h1>Hi HubToLearn</h1>
    <a href="link text">Document Object Model</a>

Properties of Javascript Document Object

An object's property, as we understand it, is the value assigned to it. The following notation is used to access the property:


Where objectName is the object's name & propertyName is the property's name

Now, in the table below, we'll show  the attributes of the document object:

domainreturns the server's domain name from which the document came.
cookieproduces a report including all unexpired and visible cookies linked with the document.
documentModereturns the mode in which the browser processed the document.
lastModifiedreturns the last modification date of a document
readyStatereturns the document's loading status.
titlereturns the HTML document's name defined between the TITLE element's beginning and closing tags.
referrerreturns the URLs of the documents that are mentioned in an HTML document.
URLreturns the HTML document's entire URL.


<!doctype html>
			/* CSS will be here */
		<title>Properties of the Document</title>

                        document.write(document.lastModified +"<br>")
			document.write(document.domain +"<br>")
                        document.write(document.title +"<br>")
			document.write(document.documentMode +"<br>")
			document.write(document.url +"<br>")


Object Methods of Javascript Documents

The JavaScript Document object also has several methods for accessing HTML elements.

open() displays the output in an HTML document.
close()closes HTML documents
write()a function that inserts a JS code or HTML expressions into a HTML doc.
writeln()After every JS code or HTML expression, it adds a new line character.
getElementById()The element with the specified id value is returned.
getElementByName()The elements with specified name values are returned.
getElementsByTagName()All items with the supplied tagname are returned

Read More about javascript