SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5
FLYMAN TECHNOLOGY LIMITED
 
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
tutorialsruby
 

Tendances (20)

introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5
 
Document object model
Document object modelDocument object model
Document object model
 
JavaScript DOM Manipulations
JavaScript DOM ManipulationsJavaScript DOM Manipulations
JavaScript DOM Manipulations
 
Dom
Dom Dom
Dom
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Xml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh malothXml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh maloth
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
DOM Quick Overview
DOM Quick OverviewDOM Quick Overview
DOM Quick Overview
 
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
 
KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7
 
Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2
 
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
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
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
 
JavaScript and DOM
JavaScript and DOMJavaScript and DOM
JavaScript and DOM
 
lect9
lect9lect9
lect9
 
Internet and Web Technology (CLASS-6) [BOM]
Internet and Web Technology (CLASS-6) [BOM] Internet and Web Technology (CLASS-6) [BOM]
Internet and Web Technology (CLASS-6) [BOM]
 
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
 

En vedette

DOM ( Document Object Model )
DOM ( Document Object Model )DOM ( Document Object Model )
DOM ( Document Object Model )
ITSTB
 

En vedette (12)

學習JavaScript_Dom
學習JavaScript_Dom學習JavaScript_Dom
學習JavaScript_Dom
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
JavaScript regular expression
JavaScript regular expressionJavaScript regular expression
JavaScript regular expression
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
 
DOM ( Document Object Model )
DOM ( Document Object Model )DOM ( Document Object Model )
DOM ( Document Object Model )
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
Javascript
JavascriptJavascript
Javascript
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 

Similaire à JavaScript DOM & event

Similaire à JavaScript DOM & event (20)

lect9
lect9lect9
lect9
 
Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
 
Javascript DOM
Javascript DOMJavascript DOM
Javascript DOM
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
Part 7
Part 7Part 7
Part 7
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Xml part 6
Xml part 6Xml part 6
Xml part 6
 
jQuery - Chapter 1 - Introduction
 jQuery - Chapter 1 - Introduction jQuery - Chapter 1 - Introduction
jQuery - Chapter 1 - Introduction
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Getting started with jQuery
Getting started with jQueryGetting started with jQuery
Getting started with jQuery
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Html dom & j query
Html dom & j queryHtml dom & j query
Html dom & j query
 
Web component driven development
Web component driven developmentWeb component driven development
Web component driven development
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 

Dernier

Dernier (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

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: "lim.borey@gmail.com", 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