SlideShare a Scribd company logo
JavaScript
DOM & Event
Open Tech Talk, 22nd January 2011
        @HackerSpacePP
About Me
var presenter = {
    name: "Lim Borey",
    workAt: "Yoolk Inc.",
    email: "lim.borey@gmail.com",
    twitter: "@limborey",
    communities: [
        "ShareVisionTeam",
        "Pailin"
    ]
};
DOM Overview
• DOM: Document Object Model - popular way of
  representing XML documents
• Implemented in many languages:
   o   Java,
   o   Perl,
   o   PHP,
   o   Ruby,
   o   Python,
   o   JavaScript
   o   …

• was constructed to provide an intuitive way for
  developers to navigate an XML hierarchy
DOM Overview
<table border="2">
    <tr>
         <td>cell is   row 0 column 0</td>
         <td>cell is   row 0 column 1</td>
    </tr>
    <tr>
         <td>cell is   row 1 column 0</td>
         <td>cell is   row 1 column 1</td>
    </tr>
</table>
DOM Overview
Waiting for the HTML
       DOM to Load
• HTML is parsed.
• External scripts/style sheets are loaded.
• Scripts are executed as they are parsed in the
  document.
• HTML DOM is fully constructed.
• Images and external content are loaded.
• The page is finished loading.
Navigating the DOM
• The document node
Navigating the DOM
<html>
    <head>
        <title>JavaScript and DOM Interfaces </title>
        <script>
            function start() {
                 myBody = document.getElementsByTagName("body")[0];
                 myBodyElements = myBody.getElementsByTagName("p");
                 myP = myBodyElements[1];
            }

        </script>
    </head>
    <body onload="start()">
        <p>hi</p>
        <p>hello</p>
    </body>
</html>
Navigating the DOM
Navigating the DOM
• All node
Navigating the DOM
<p>
       <strong>Hello</strong>
       how are you doing?
</p>
Creating Node


var textNode = document.createTextNode("world");

var myNewPNode = document.createElement("p");
Attaching, copying or
  removing nodes
Attaching, copying or
     removing nodes
myP.appendChild(myTextNode);
Node information
Event
• an action that is fired (initiated) within a web page.
• JavaScript is Single Thread
• JavaScript uses asynchronous callback




 What you’d see if JavaScript were   A representation of using callbacks
     able to handle threads              to wait for the page to load
Event Phases
Defining Event Handler
• Old way
window.onload = init();

• New way (add event)
window.addEventListener("load", init, false);
window.attachEvent("onload", init); //IE
Event Methods
• Adding and removing event listener
The Event Object
• Contains contextual information about the current
  event
• an object that’s provided, or is available, inside of
  every event handler function
• W3C Standard Browser: e
• Internet Explorer: window.event

textArea.onkeypress = function(e){
    e = e || window.event;
    return e.keyCode != 13;
};
Cancel Bubbling
• W3C Standard Browser
      e.stopPropagation()
• Internet Explorer
      window.event.cancelBubble()
Overriding Browser
       default event
• W3C Standard Broswer:
      e.preventDefault();
• Internet Explorer
      window.event.returnValue = false;
References
• Pro JavaScript™ Techniques, John Resig

• http://www.howtocreate.co.uk/tutorials/javascript/
  domstructure

• https://developer.mozilla.org/en/Traversing_an_HTM
  L_table_with_JavaScript_and_DOM_Interfaces

More Related Content

PPT
Document Object Model
PPT
JavaScript & Dom Manipulation
PPT
Document Object Model
PPTX
Dom(document object model)
PDF
Javascript, DOM, browsers and frameworks basics
PDF
Interacting with the DOM (JavaScript)
PPTX
Javascript inside Browser (DOM)
Document Object Model
JavaScript & Dom Manipulation
Document Object Model
Dom(document object model)
Javascript, DOM, browsers and frameworks basics
Interacting with the DOM (JavaScript)
Javascript inside Browser (DOM)

What's hot (20)

PPT
introduction to the document object model- Dom chapter5
PPTX
Document object model
PDF
JavaScript DOM Manipulations
PPTX
PDF
JavaScript and BOM events
PPT
JavaScript: Ajax & DOM Manipulation
PPT
JavaScript
PPTX
Document Object Model
PPT
Xml dom & sax by bhavsingh maloth
PPTX
An Introduction to the DOM
PPT
DOM Quick Overview
PPTX
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
PPT
KMUTNB - Internet Programming 4/7
PPT
Javascript: Ajax & DOM Manipulation v1.2
PPTX
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
PPTX
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
PDF
JavaScript and DOM
PDF
PPTX
Internet and Web Technology (CLASS-6) [BOM]
PDF
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
introduction to the document object model- Dom chapter5
Document object model
JavaScript DOM Manipulations
JavaScript and BOM events
JavaScript: Ajax & DOM Manipulation
JavaScript
Document Object Model
Xml dom & sax by bhavsingh maloth
An Introduction to the DOM
DOM Quick Overview
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
KMUTNB - Internet Programming 4/7
Javascript: Ajax & DOM Manipulation v1.2
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
JavaScript and DOM
Internet and Web Technology (CLASS-6) [BOM]
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Ad

Viewers also liked (12)

PDF
學習JavaScript_Dom
PDF
javascript objects
PPT
JavaScript Objects
PDF
JavaScript regular expression
PPTX
Javascript validating form
PPTX
Form Validation in JavaScript
PPTX
Document object model(dom)
PPT
DOM ( Document Object Model )
PDF
Javascript and DOM
PPTX
Javascript
PPT
Regular Expressions
PPTX
Introduction to Regular Expressions
學習JavaScript_Dom
javascript objects
JavaScript Objects
JavaScript regular expression
Javascript validating form
Form Validation in JavaScript
Document object model(dom)
DOM ( Document Object Model )
Javascript and DOM
Javascript
Regular Expressions
Introduction to Regular Expressions
Ad

Similar to JavaScript DOM & event (20)

PPT
The Theory Of The Dom
PPTX
Modern Web Technologies
PPT
Scripting The Dom
PDF
Fast Cordova applications
PPT
Document_Object_Model_in_javaScript..................................ppt
PPTX
JS basics
PPTX
Web technologies-course 09.pptx
PDF
Intro to JavaScript
PDF
DOM Performance (JSNext Bulgaria)
PPTX
INFT132 093 07 Document Object Model
PDF
Introduction to js (cnt.)
PDF
Javascript projects Course
PPTX
High performance websites session1
PPTX
Learning About JavaScript (…and its little buddy, JQuery!)
PPTX
Javascript 2
PDF
Speeding up mobile web apps
PDF
Java script
PDF
Fast mobile web apps
PPTX
DOM and Events
PDF
Advanced guide to develop ajax applications using dojo
The Theory Of The Dom
Modern Web Technologies
Scripting The Dom
Fast Cordova applications
Document_Object_Model_in_javaScript..................................ppt
JS basics
Web technologies-course 09.pptx
Intro to JavaScript
DOM Performance (JSNext Bulgaria)
INFT132 093 07 Document Object Model
Introduction to js (cnt.)
Javascript projects Course
High performance websites session1
Learning About JavaScript (…and its little buddy, JQuery!)
Javascript 2
Speeding up mobile web apps
Java script
Fast mobile web apps
DOM and Events
Advanced guide to develop ajax applications using dojo

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Network Security Unit 5.pdf for BCA BBA.
Understanding_Digital_Forensics_Presentation.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
MIND Revenue Release Quarter 2 2025 Press Release
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
Teaching material agriculture food technology

JavaScript DOM & event

  • 1. JavaScript DOM & Event Open Tech Talk, 22nd January 2011 @HackerSpacePP
  • 2. About Me var presenter = { name: "Lim Borey", workAt: "Yoolk Inc.", email: "[email protected]", twitter: "@limborey", communities: [ "ShareVisionTeam", "Pailin" ] };
  • 3. DOM Overview • DOM: Document Object Model - popular way of representing XML documents • Implemented in many languages: o Java, o Perl, o PHP, o Ruby, o Python, o JavaScript o … • was constructed to provide an intuitive way for developers to navigate an XML hierarchy
  • 4. DOM Overview <table border="2"> <tr> <td>cell is row 0 column 0</td> <td>cell is row 0 column 1</td> </tr> <tr> <td>cell is row 1 column 0</td> <td>cell is row 1 column 1</td> </tr> </table>
  • 6. Waiting for the HTML DOM to Load • HTML is parsed. • External scripts/style sheets are loaded. • Scripts are executed as they are parsed in the document. • HTML DOM is fully constructed. • Images and external content are loaded. • The page is finished loading.
  • 7. Navigating the DOM • The document node
  • 8. Navigating the DOM <html> <head> <title>JavaScript and DOM Interfaces </title> <script> function start() { myBody = document.getElementsByTagName("body")[0]; myBodyElements = myBody.getElementsByTagName("p"); myP = myBodyElements[1]; } </script> </head> <body onload="start()"> <p>hi</p> <p>hello</p> </body> </html>
  • 11. Navigating the DOM <p> <strong>Hello</strong> how are you doing? </p>
  • 12. Creating Node var textNode = document.createTextNode("world"); var myNewPNode = document.createElement("p");
  • 13. Attaching, copying or removing nodes
  • 14. Attaching, copying or removing nodes myP.appendChild(myTextNode);
  • 16. Event • an action that is fired (initiated) within a web page. • JavaScript is Single Thread • JavaScript uses asynchronous callback What you’d see if JavaScript were A representation of using callbacks able to handle threads to wait for the page to load
  • 18. Defining Event Handler • Old way window.onload = init(); • New way (add event) window.addEventListener("load", init, false); window.attachEvent("onload", init); //IE
  • 19. Event Methods • Adding and removing event listener
  • 20. The Event Object • Contains contextual information about the current event • an object that’s provided, or is available, inside of every event handler function • W3C Standard Browser: e • Internet Explorer: window.event textArea.onkeypress = function(e){ e = e || window.event; return e.keyCode != 13; };
  • 21. Cancel Bubbling • W3C Standard Browser e.stopPropagation() • Internet Explorer window.event.cancelBubble()
  • 22. Overriding Browser default event • W3C Standard Broswer: e.preventDefault(); • Internet Explorer window.event.returnValue = false;
  • 23. References • Pro JavaScript™ Techniques, John Resig • http://www.howtocreate.co.uk/tutorials/javascript/ domstructure • https://developer.mozilla.org/en/Traversing_an_HTM L_table_with_JavaScript_and_DOM_Interfaces