• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
DOM
 

DOM

on

  • 1,254 views

 

Statistics

Views

Total Views
1,254
Views on SlideShare
1,254
Embed Views
0

Actions

Likes
2
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    DOM DOM Presentation Transcript

    • Ajax, JavaScript and PHP Chapter 5 – Document Object Model (DOM)
    • JavaScript Supports three kinds of objects
      • Built-in objects: are built in to the JavaScript language. (Date, Array, String)
      • DOM (Document Object Model): objects represent various components of the browser and the current HTML document.
      • Custom objects: objects you created.
      • NOTE: DOM is not part of the JavaScript language. It is an API (Application Programming Interface) build in to the browser.
      • NOTE: The window object is the parent object for all of the objects. (See next diagram)
    • The DOM object hierarchy
    • HTML Document Object Model (HTML DOM)
    • Scripting Documents
      • Every web browser window (or frame) displays an HTML document. The Window object that represents that window has a document property that refers to a Document object.
      • HTML documents can contain text, images, hyperlinks, form elements, and so on.
      • JavaScript code can access and manipulate the objects that represent each document element.
      • A Document Object Model, or DOM, is an API that defines how to access the objects that compose a document.
      • The W3C defines a standard DOM that is reasonably well supported in all modern web browsers.
      • In the early days, Netscape 2 and 3 supported a simple DOM that provided access only to special document elements such as links, images, and form elements. This legacy DOM was adopted by all browser vendors and has been formally incorporated into the W3C standard as the “Level 0” DOM.
    • Scripting Documents
      • With IE 4, has a new DOM, it allowed access to all elements in a document. Microsoft’s API is known as the IE 4 DOM. It was never standardized, and IE 5 and later adopted the W3C DOM.
      • Netscape 4 based on dynamically positioned scriptable elements known as layers . It was supported only in Netscape 4 and dropped from the Mozilla and Firefox browsers.
      • The W3C DOM standard, or simply, DOM, adopted IE and Netscape DOM.
    • Dynamic Document Content
      • Let’s exploring the Document object with its write() method. This method is part of the legacy DOM:
        • <script>
        • var today = new Date();
        • document.write(“<p>Document access on: “ + today.toString());
        • </script>
      • You might create a pop-up window and write some HTML to it with code like this:
        • function hello() {
        • var w = window.open();
        • var d = w.document;
        • d.open(); //open a new document – optional
        • d.write(“<h1>Hello world!</h1>”);
        • d.close();
        • }
      • If you call the write() method on a document that has already been closed, JavaScript implicitly opens a new HTML document.
    • Document Properties
      • Legacy DOM Document object properties:
        • bgColor (deprecated)
        • cookie
        • domain
        • lastModified
        • location
        • referrer
        • title
        • URL
      • You can place code like this to the bottom of each web document:
        • <hr>
        • document: <script>document.write(document.title);</script><br>
        • URL: <script>document.write(document.URL);</script><br>
        • Last Update: <script>document.write(document.lastModified);</script>
    • Legacy DOM: Document Object Collections
      • The Document Object Collections, are array-valued properties, are the heart of the legacy DOM. They are the properties that give you access to certain especial elements of the document:
        • anchors[ ]
        • applets[ ]
        • forms[ ]
        • images[ ]
        • links[ ]
      • Their elements are in the same order as in the document source code. document.forms[0] refers to the first <form> tag in the document.
    • Naming Document Objects
      • In the legacy DOM, with the name attribute, you can assign names to important document elements and to refer to those elements by name:
        • <form name=“f1”><input type=“button” value=“Push Me”></form>
      • Assuming that the <form> is the first one in the document:
        • document.forms[0] // refer to the form by position
        • document.f1 // refer to the form by name
        • document.forms[“f1”] // refer to the form as array index
      • Setting the name attribute of a <form>, <img>, or <applet> (but not of <a> and <link>>) also makes the corresponding Form, Image, or Applet object accessible as a named property of the document object itself. Thus, you can also refer to the form as:
          • document.f1
      • You have a form that looks like this:
        • <form name=“shipping”>
        • <input type=“text” name=“zipcode”>
        • </form>
        • /* you can refer to the text input field element as : */
        • document.shipping.zipcode
    • Overview of the W3C DOM
      • HTML documents have a hierarchical structure of nested tags that is represented in the DOM as a tree of objects. The tree representation of an HTML document contains nodes representing HTML tags or elements, such as <body> and <p>.
      • Consider the following simple HTML document:
        • <html>
        • <head><title>Simple Document</title>
        • </head>
        • <body>
        • <h1>An HTML Document</h1>
        • <p>This is a <i>simple</i> document.
        • </body>
        • </html>
    • DOM Levels and Features
      • There are two versions, or “levels” of the DOM standard.
      • DOM Level 1 was standardized in October 1998
      • DOM Level 2 was standardized in November 2000
      • DOM Level 3 features are not yet widely supported by web browsers.
    • Finding Elements in a Document
      • When programming with the DOM API, it is quite common to need a particular node within the document or a list of nodes of a specific type within the document.
      • The Document object is the root of every DOM tree, but it does not represent an HTML element in that tree.
      • The document.documentElement property refers to the <html> tag that serves as the root element of the document.
      • The body property of the HTML Document object is a convenient special-case property:
        • document.getElementsByTagName(“body”)[0]
      • This expression calls the Document object’s getElementsByTagName() method and selects the first element of the returned array.
      • You can use getElementsByTagName() to obtain a list of any type of HTML element.
        • var tables = document.getElementsByTagName(“table”);
        • alert(“This document contains “ + tables.length + “ tables”);
    • Finding Elements in a Document
      • If you pass the special string “*” to getElementsByTagName(), it returns a list of all elements in the document, in the order in which they appear. (this special usage is not supported in IE 5 and IE 5.5, see IE specific HTMLDocument.all[ ])
      • To get the fourth paragraph in a document:
        • var myParagraph = document.getElementsByTagName(“p”)[3];
      • This is not the best nor the most efficient technique.
      • It is best to give elements an id attribute, then look up your desired element by its ID.
        • <p id = “specialParagraph”>
        • var myParagraph = document.getElementById(“specialParagraph”);
    • Window Object – a clock
      • <html>
      • <head>
      • <script language=“JavaScript”>
      • function startTime()
      • {
      • var today=new Date();
      • var h=today.getHours();
      • var m=today.getMinutes();
      • var s=today.getSeconds();
      • document.getElementById('txt').innerHTML=h+&quot;:&quot;+m+&quot;:&quot;+s;
      • t=setTimeout('startTime()',500);
      • }
      • </script>
      • </head>
      • <body onload=&quot;startTime()&quot;>
      • <div id=&quot;txt&quot;></div>
      • </body>
      • </html>
    • Window Object - URL
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function currLocation()
      • {
      • alert(window.location);
      • }
      • function newLocation()
      • {
      • window.location=&quot;http://www.ccsf.edu&quot;;
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; onclick=&quot;currLocation()&quot; value=&quot;Show current URL&quot;>
      • <input type=&quot;button&quot; onclick=&quot;newLocation()&quot; value=&quot;Change URL&quot;>
      • </body>
      • </html>
    • Window Object – Reload and Print
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function reloadPage()
      • {
      • window.location.reload();
      • }
      • function printpage()
      • {
      • window.print();
      • }
      • </script>
      • </head>
      • <body>
      • <input type=&quot;button&quot; value=&quot;Reload page&quot; onclick=&quot;reloadPage()&quot; />
      • <input type=&quot;button&quot; value=“Print this page&quot; onclick=“printpage()&quot; />
      • </body>
      • </html>
    • Document Object – Title, URL
      • <html>
      • <head>
      • <title>My title</title>
      • </head>
      • <body>
      • <script type=&quot;text/javascript&quot;>
      • document.writeln(“Document Title: “, document.title);
      • document.writeln(“Document URL: “, document.URL);
      • </script>
      • </body>
      • </html>
    • Document Object - getElementById
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function getValue()
      • {
      • var x=document.getElementById(&quot;myHeader&quot;);
      • alert(x.innerHTML);
      • }
      • </script>
      • </head>
      • <body>
      • <h1 id=&quot;myHeader&quot; onclick=&quot;getValue()&quot;>This is a header</h1>
      • <p>Click on the header to alert its value</p>
      • </body>
      • </html>
      • Result: will display This is a header
    • Document Object - getElementsByName
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function getElements()
      • {
      • var x=document.getElementsByName(&quot;myInput&quot;);
      • alert(x.length);
      • }
      • </script>
      • </head>
      • <body>
      • <input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot; /><br />
      • <input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot; /><br />
      • <input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot; /><br />
      • <br />
      • <input type=&quot;button&quot; onclick=&quot;getElements()&quot; value=&quot;How many elements named 'myInput'?&quot; />
      • </body>
      • </html>
      • Result: will display 3
    • Anchor Object – href, target
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function changeLink()
      • {
      • document.getElementById('myAnchor').innerHTML=&quot;Visit CCSF&quot;;
      • document.getElementById('myAnchor').href=&quot;http://www.ccsf.edu&quot;;
      • document.getElementById('myAnchor').target=&quot;_blank&quot;;
      • }
      • </script>
      • </head>
      • <body>
      • <a id=&quot;myAnchor&quot; href=&quot;http://www.microsoft.com&quot;>Visit Microsoft</a>
      • <input type=&quot;button&quot; onclick=&quot;changeLink()&quot; value=&quot;Change link&quot;>
      • <p>In this example we change the text and the URL of a hyperlink. We also change the target attribute.
      • The target attribute is by default set to &quot;_self&quot;, which means that the link will open in the same window.
      • By setting the target attribute to &quot;_blank&quot;, the link will open in a new window.</p>
      • </body>
      • </html>
    • Image Object – resize, change src
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function changeSize()
      • {
      • document.getElementById(“ccsf&quot;).height=&quot;250&quot;;
      • document.getElementById(“ccsf&quot;).width=&quot;300&quot;;
      • }
      • function changeSrc()
      • {
      • document.getElementById(“ccsf&quot;).src=“colan.gif&quot;;
      • }
      • </script>
      • </head>
      • <body>
      • <img id=“ccsf&quot; src=“logo_01.gif&quot; width=&quot;107&quot; height=&quot;98&quot; />
      • <br /><br />
      • <input type=&quot;button&quot; onclick=&quot;changeSize()&quot; value=&quot;Change height and width of image&quot;>
      • <input type=&quot;button&quot; onclick=&quot;changeSrc()&quot; value=&quot;Change image&quot;>
      • </body>
      • </html>
    • Table Object – update cells
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function changeContent()
      • {
      • var x=document.getElementById('myTable').rows[0].cells;
      • x[0].innerHTML=&quot;NEW CONTENT&quot;;
      • x[1].innerHTML=&quot;NEW TOO&quot;;
      • }
      • </script>
      • </head>
      • <body>
      • <table id=&quot;myTable&quot; border=&quot;1&quot;>
      • <tr><td>Row1 cell1</td><td>Row1 cell2</td></tr>
      • <tr><td>Row2 cell1</td><td>Row2 cell2</td></tr>
      • <tr><td>Row3 cell1</td><td>Row3 cell2</td></tr>
      • </table>
      • <form>
      • <input type=&quot;button&quot; onclick=&quot;changeContent()&quot; value=&quot;Change content&quot;>
      • </form>
      • </body>
      • </html>
    • Table Object – display cell
      • <html>
      • <head>
      • <script type=&quot;text/javascript&quot;>
      • function dsptxt(id)
      • {
      • alert(document.getElementById(id).innerHTML);
      • }
      • </script>
      • </head>
      • <body>
      • <table id=&quot;mytable&quot; border=&quot;2&quot;>
      • <tr><td id=&quot;cell1&quot;><a href=&quot;javascript:dsptxt('cell1');&quot;>Cell 1 data</a></td>
      • <td id=&quot;cell2&quot;><a href=&quot;javascript:dsptxt('cell2');&quot;>Cell 2 data</a></td></tr>
      • <tr><td id=&quot;cell3&quot;><a href=&quot;javascript:dsptxt('cell3');&quot;>Cell 3 data</a></td>
      • <td id=&quot;cell4&quot;><a href=&quot;javascript:dsptxt('cell4');&quot;>Cell 4 data</a></td></tr>
      • <tr><td id=&quot;cell5&quot;><a href=&quot;javascript:dsptxt('cell5');&quot;>Cell 5 data</a></td>
      • <td id=&quot;cell6&quot;><a href=&quot;javascript:dsptxt('cell6');&quot;>Cell 6 data</a></td></tr>
      • <tr><td id=&quot;cell7&quot;><a href=&quot;javascript:dsptxt('cell7');&quot;>Cell 7 data</a></td>
      • <td id=&quot;cell8&quot;><a href=&quot;javascript:dsptxt('cell8');&quot;>Cell 8 data</a></td></tr>
      • </table>
      • </body>
      • </html>