The document object is an object that is created by the browser for each HTML page (document) that is viewed.
The Document object is a property of the Window object, and can be accessed through the window.document, but, becouse the "window" is the main object, it can be omited.
DOM stands for "Document Object Model", it is a hierarchy of data types for HTML documents, links, forms, tags, and everything else that can be represented in HTML code.
DOM allows JavaScript to access the structure of the document in the browser.

Document Object Properties


- Exemples with some of these properties:
1) Display and change the text from TITLE meta-tag
<script type="text/javascript"><!--
document.write(document.title);         // Document Object - DOM
document.title = 'New title';         // "New title" will appear in the tittle bar
--></script>

2) In the following example, the JS script gets and writes the domain and the complete URL of the page.
<script type="text/javascript"><!--
var domain_site = document.domain;
var url = document.URL;

document.write(domain_site+ '<br />');         // coursesweb.net
document.write(url);         // http://coursesweb.net/javascript/document-object-dom
--></script>

3) In the next example three link tags are defined and a JS script writes the address from the second link.
<a href="http://coursesweb.net" title="Courses Web">CoursesWeb</a><br />
<a href="http://www.marplo.net" title="Lessons, Tutorials">Lessons, Tutorials</a><br />
<a href="http://www.yahoo.com" title="yahoo.com">yahoo.com</a><br />
<script type="text/javascript"><!--
var adrs = document.links;      //  gets all links from <a> tags

document.write(adrs[1]);         // http://www.marplo.net/
--></script>

Document Object Methods


• You can get the content of a HTML tag, when you know its ID, by using one of the fallowing sintaxes:
variable_name = document.getElementById("id").innerHTML;
      or
variable_name = document.getElementById("id").childNodes[0].nodeValue;

• To change or add a content inside a HTML that has an ID, use the fallowing expresion:
document.getElementById("id").innerHTML = "content";

- Exemples with some of these methods:
1) The following script displays an alert with the text added in a tag with id="an_id"
<div id="an_id">JS lessons and tutorials</div>
<script type="text/javascript"><!--
var str = document.getElementById('an_id');
alert(str.innerHTML);          // JS lessons and tutorials
--></script>

2) In the next example, when you click on the text "DOM JavaScript lesson", it is called a function that change that content, using the ID of that tag (transfered as a parameter).
- the expresion this.id gets the value of the ID attribute added in own element.
<script type="text/javascript"><!--
function changeTxt(id) {
  document.getElementById(id).innerHTML = "<u>new content</u>";
}
--></script>
<div id="ex2" onclick="changeTxt(this.id)">DOM JavaScript lesson</div>
- It will displays the fallowing rezult, click on the text to see the effect.
DOM JavaScript lesson

3) The next example uses the getElementsByTagName() method and return an alert with the number of LI tags
<script type="text/javascript"><!--
function nrElements(tag) {
  var tags = document.getElementsByTagName(tag);
  alert(tags.length);
}
--></script>
<ul>
 <li>Courses</li>
 <li>Lessons</li>
 <li>tutorials</li>
</ul>
<button onclick="nrElements('li')">How many LI tags</button>
- Click the button to see the result.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Document Object - DOM

Last accessed pages

  1. Create simple Website with PHP (15204)
  2. Zuma Games (17987)
  3. Flash Games - Free online Games (16913)
  4. Table Tennis (413)
  5. Sport Games (7119)

Popular pages this month

  1. Qwop (6860)
  2. Drag Racer V3 (4691)
  3. Bubbles3 (1916)
  4. List with All the Games (1605)
  5. Butterfly Kyodai (1507)