1 / 20

DOM

DOM. Photo from http://www.flickr.com/photos/emraya/2861149369/. Documents as Objects. DOM. When a browser reads an HTML file, it must interpret the file and render it onscreen. This process is sophisticated.

tory
Download Presentation

DOM

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DOM Photo from http://www.flickr.com/photos/emraya/2861149369/ Documents as Objects

  2. DOM • When a browser reads an HTML file, it must interpret the file and render it onscreen. This process is sophisticated. • The DOM is an abstract model that defines the interface between HTML text and applications that need to process HTML . The Tree is a DOM instance. graphics from Yahoos Douglas Crockford.

  3. DOM • The DOM is a model of the HTML document. • Standardized by w3c • A definition of the objects and supported methods • Standards • DOM 0 is supported by all JavaScript enabled browsers but has no specification. • DOM 1 was released in 1998 • DOM 2 was released in 2000 • DOM 3 is the latest approved standard

  4. DOM • From W3C: • 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. The document can be further processed and the results of that processing can be incorporated back into the presented page. • "Dynamic HTML" is a term used by some vendors to describe the combination of HTML, style sheets and scripts that allows documents to be animated. graphics from Yahoos Douglas Crockford.

  5. DOM Data Types • I recommend checking out. Some of the API descriptors in this presentation are taken from that site. • https://developer.mozilla.org/en/DOM/document#Introduction

  6. Overview document document. documentElement document.body The DOM specifies that a variable named "document" is globally accessible. This variable has certain attributes. graphics from Yahoos Douglas Crockford.

  7. Document • The document variable is the HTML document. This variable has a number of convenience properties. Here are some of them: • document.anchors • document.applets • document.embeds • document.forms • document.frames • document.images • document.plugins • document.scripts • document.stylesheets • These are all collections of things. They are either deprecated or are generally viewed as poor alternatives.

  8. Elements and Tree Structure

  9. Elements and Tree Structure

  10. Elements and Tree Structure

  11. Finding an ELement • By the id of the element • var node = document.getElementById('x13'); • By the class name • var node = document.getElementsByClassName('well'); • By the tag name • var node = document.getElementsByTagName('div'); • By the name • var node = document.getElementsByName('movies');

  12. Element Properties • Elements have properties with values that can be programmatically changed. • node.setAttribute('propName', propValue); • Examples • node.setAttribute('style', 'background-color:red;'); • node.setAttribute('id', 'x83');

  13. Creating Nodes in the DOM • You must go through a document (i.e. the root element) in order to create nodes that can be used in that document. • document.createElement(tagName) • document.createTextNode(text)

  14. Creating/Deleting Nodes in the DOM • The new nodes are not part of the document. They have simply been created. • node.appendChild(newNode) • node.insertBefore(newNode, newNodesSibling) • How to replace a child node on a node? • node.parentNode.replaceChild(newNode, node) • How to delete a node in the DOM • node.removeNode(childNodeToRemove) • It is often helpful to take a chunk of HTML and replace it by some other chunk of HTML. This can be done by • node.innerHTML = newHTMLtext.

  15. Mouse Events

  16. HTML Form Events http://en.wikipedia.org/wiki/DOM_events

  17. Registering Handlers • The supported attributes of a DOM element determine what events the element can respond to • The attribute can be directly set to a script (using a script). <head> <script type="text/javascript"> function helloWorld() { window.alert( "Hello World" ); } window.onload = helloWorld; // register a handler document.onclick = helloWorld; // register a handler document.onclick = null; // remove a handler </script> </head> <body> <p>Hello World!</p> </body>

  18. Registering Handlers • Elements can also have the handler specified via HTML • <element onclick=“dothis()” /> • Write an HTML page that contains text elements. Whenever the user clicks on a text element, the background color of that element changes to a randomly determined value. As an added feature – have some text elements jump around on the screen when clicked in addition to changing their color

  19. DOM Events Example • Develop an HTML page that allows users to order a pizza. Use ‘onclick’ handling for the radio buttons • Users must select one topping • Sardines • Anchovies • Lemons • Pumpernickel • Users must select one cheese • Provolone • American • Blue • Cheddar • Some combinations of topping/cheese are so repugnant that it would be unethical to sell them. Users receive a warning if selecting unethical combinations. The selection is nonetheless allowed on the page. • Sardines and Cheddar • Pumpernickel and Blue • Lemons and Blue • Alternately, warn and disallow unethical combinations

  20. JavaScript Dom Methods • User interaction (these functions block) • alert( text ) • confirm( text ) • prompt( text, default ) • Timed function calls • delayed = setTimeout( func, msec ) • executes func after msec milliseconds • repeating = setInterval( func, msec ) • executes func every msec milliseconds • clearTimeout( delayed ) • stops the timeout • clearInterval( repeating ) • stops the interval

More Related