Open In App

ES6 HTML DOM

DOM stands for Document Object Model. An HTML page is rendered in a browser. The browser assembles all the elements contained in the page, downloaded from the webserver, in its memory. Once this is done, the browser then displays these objects in the browser window and once that is done, the browser can no longer recognize individual HTML elements. JavaScript enabled browsers are capable of recognizing individual objects in an HTML page after the page has been rendered in the browser because JavaScript enabled browser recognizes and uses DOM. Thus, allowing the functionality of the object to be controlled at will.

Advantages of DOM: 



Structure  



DOM Categories: There are three types of DOM supported by JavaScript but we will only discuss those that are applicable with ES6.

1. Legacy DOM: This was the model used by early versions of JavaScript. This model provides read-only properties such as title, URL, etc. It also provides with lastModified information about the document as a whole. This model has a lot of methods that can be used to set and get the document property value.

Document properties of Legacy DOM:  

Eg. document.alinkColor
Eg. document.anchors[0],document.anchors[1],...
Eg. document.applets[0],document.applets[1],..
Eg. document.bgColor
Eg. document.cookie
Eg. document.domain
Eg. document.embeds[0],document.embeds[1],...
Eg. document.fgColor
Eg. document.forms[0],document.forms[1],...
Eg. document.images[0[,document.images[1],...
Eg. document.lastModified
Eg. document.linkColor
Eg. document.links[0],document.links[1],...
Eg. document.location
Eg. document.plugins[0],document.plugins[1],...
Eg. document.referrer
Eg. document.title
Eg. document.URL
Eg. document.vlinkColor

Document methods in Legacy DOM:  

Eg. document.clear()
Eg. document.close()
Eg. document.open()
Eg. document.write()
Eg. document.writeln()

Example:  




<!DOCTYPE html>
<html>
 
<head>
    <title>Legacy DOM example</title>
    <script type="text/javascript">
        function func1() {
            var a = document.title;
            alert("Document title: " + a);
 
        }
 
        function func2() {
            var b = document.URL;
            var c = document.lastModified;
            var d = document.location;
 
            document.write("Document URL: " + b);
            document.write("Document last modified: " + c);
            document.write("Document location: " + d);
 
        }
 
        function func3() {
            var n = document.forms[1];
            document.write("Second form elements: " + n);
        }
    </script>
</head>
 
<body>
    <center>
        <h1 style="color: green">GeeksforGeeks</h1>
        <b>Document to try Legacy DOM elements.</b>
        <form name="form1">
             
 
 
<p>Section 1:</p>
 
 
 
            <button name="b1" id="1"
                    value="title" onclick="func1()">
              Title
            </button>
            <button name="b2" id="2"
                    value="INFO" onclick="func2()">
              URL
            </button>
        </form>
        <br>
 
        <form name="form2">
            Section 2:
            <br>
            <br>
            <button name="bt1" id="1"
                    value="submit" onclick="func3()">
              Elements
            </button>
        </form>
  </center>
</body>
 
</html>

Output: 

2. W3C DOM: This DOM follows the standard World Wide Web Consortium which says:  

“The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure, and style of documents.”

This model focuses mostly on language neutrality so that scripting and styling documents can be made easy.

Document properties in W3C DOM:  

Eg. document.body
Eg. document.defaultView
Eg. document.documentElement
Eg. document.implementation

Documents methods in W3C DOM:  

Eg. document.createAttribute(name_of_attr)
Eg. document.createComment(some_text)
Eg. document.createDocumentFragment()
Eg. document.createElement(tagname_of_new_ele)
Eg. document.createTextNode(text)
Eg. document.getElementById(Id)
Eg. document.getElementsByName(name)
Eg. document.getElementsByTagName(tagname)
Eg. document.importNode(importedNode, deep)

Example:  




<!DOCTYPE html>
<html>
 
<head>
    <title>W3c example</title>
    <script type="text/javascript">
        function hello() {
            var n = document.getElementById('2').value;
            document.write("Hello " + n);
 
            var a = document.body;
            document.write("</br>Document body:" + a);
 
            var b = document.defaultView;
            document.write("</br>Document default view: " + b);
 
        }
    </script>
</head>
 
<body>
    <center>
 
        <h1 style="color: green">
          GeeksforGeeks
        </h1>
 
         
 
 
<p>
          Hello from GeeksforGeeks, this is
          an example representation.
        </p>
 
 
 
 
        <form name="myform">
            <b>Name:</b>
            <input type="text" name="name" id="2">
            <input type="submit" name="submit"
                   value="submit" onclick="hello()">
 
        </form>
    </center>
 
</body>
 
</html>           

Output: 

3. IE4 DOM: This DOM was introduced in version 4 of Internet Explorer. Later versions expanded and went on to include features from W3C DOM.

Document properties in IE4 DOM: 

Eg. document.activeElement
Eg. document.all[]
 
Note
This function was used to check for the presence of specific elements in a web page, but it is now considered deprecated 
because it only works in Internet Explorer and is not supported in other browsers.
Eg. document.charset
Eg. document.children[]
Eg. document.defaultCharset
Eg. document.expando
Eg. document.parentWindow
Eg. document.readyState
Eg. document.uninitialized
Eg. document.loading
Eg. document.interactive
Eg. document.complete

Document methods in IE4 DOM:  

Eg. document.elementFromPoint(x,y)

Example:  




<!DOCTYPE html>
<html>
 
<head>
    <title>IE4 DOM example</title>
    <script type="text/javascript">
        function func1() {
            var a = document.all["greetings"];
            alert("Hey, " + a.innerHTML);
 
        }
 
        function func2() {
            var b = document.activeElement;
            document.write("Active element: " + b);
 
        }
    </script>
</head>
 
<body>
    <center>
        <h1 style="color: green">
          GeeksforGeeks
        </h1>
       
        <h4 id="greetings">
          Hello from GeeksforGeeks, this is
        an example representation.
        </h4>
       
        <form name="myform">
            <input type="submit" name="b1"
                   value="Greet" onclick="func1()">
            <input type="text" name="text1">
            <input type="submit" name="b2"
                   onclick="func2()">
        </form>
    </center>
 
</body>
 
</html>

Output: 

 


Article Tags :