SlideShare ist ein Scribd-Unternehmen logo
1 von 28
DOM Events

 Pete Frueh
What’s a Document?
The 1040 is a Document!
google.com is a Document!
yahoo.com is a Document!
facebook.com is a Document!
linkedin.com is a Document!
Prerequisite Brainwashing

• The WWW is made up of documents

• Documents can contain images, forms, links to
  other documents, embedded media, etc.

• No matter how dynamic, interactive,
  personalized, or app-like a web page is, it’s still
  just a document.
Topics
1. The DOM is the BOM

2. DOM Events and How to (At/De)tach Them

3. DOM Event Flow

4. The Event Object

5. Event Delegation
The Browser Object Model (BOM)
The interface between the browser and JavaScript
     (starting at the top-level window object)

PROPERTIES: window.navigator, window.location,
window.frames, window.history, etc.

METHODS: window.open, window.close, window.alert, etc.

EVENTS: window.onscroll, window.onresize, etc.
The Browser Object Model (BOM)

    http://jsfiddle.net/pfrueh/AXtfN/
The Document Object Model (DOM)
• “The DOM is a language- and platform neutral interface
  that allows programs and scripts to dynamically access
  and update the content and structure of documents.”

• In the browser, the document object is created
  automatically with each new HTML document; it is
  assigned to the window object:
The Document Object Model (DOM)
In the DOM, everything’s a Node, and there are 12
subclasses of Node, the most common are:

Document: window.document
Element: myEl = document.getElementById('my-div')
Attribute:
   element.setAttribute('href', 'http://www.linkedin.com')
   element.getAttribute('bar') // null if !exists on element
   myAttr = document.createAttribute('href')
Text: myText = document.createTextNode('Hello World')
The Document Object Model (DOM)


   http://jsfiddle.net/pfrueh/w4a7W/
Events: HTML
<!-- alerts a reference to the event object (discussed later) -->
<a href="#" onclick="alert(event)">...</a>

<!-- alerts a reference to the <a> element -->
<a href="#" onclick="alert(this)">...</a>

<!-- this used to be a common pattern -->
<a href="#" onclick="handleClick(event, this)">...</a>
Events: DOM Level 0
<div id="my-div">My Div</div>

var myDiv = document.getElementById('my-div');
myDiv.onclick = function(e) {
   alert(e); // alerts the event object
   alert(this); // alerts a reference to myDiv
};
// ^ what's the problem with this approach?
Events: DOM Level 2
var myA = document.getElementById('my-a');

// W3C compliant browsers
myA.addEventListener('click', handleClick, false);

// IE less than 9 still requires proprietary events :(
myA.attachEvent('onclick', handleClick);
Simple Pattern For Modern Browsers
  (Using Native JS and not a Library)
if (element.addEventListener) { //  feature/object detection
    element.addEventListener('click', clickHandler, false);
 } else if (element.attachEvent) { // Pre-IE9
    element.attachEvent('onclick', clickHandler);
 }

if (element.removeEventListener) {
    element.removeEventListener('click', clickHandler, false);
} else if (element.detachEvent) { // Pre-IE9
    element.detachEvent('onclick', clickHandler);
}
Attaching Events

http://jsfiddle.net/pfrueh/EvXmW/
How to Tell When the DOM is Loaded
document.addEventListener('DOMContentLoaded', init, false);
// ^ works for all modern browsers including IE 9+ (but not before)

http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/Default.html


// libraries will take care of the pre-IE9 issue, so use them
jQuery: $(document).ready(init) OR $(init)
YUI 2: YAHOO.Event.onDOMReady(init)
YUI 3: Y.on('domready', init)
DOM Event Flow
DOM Event Flow

http://jsfiddle.net/pfrueh/FK7QC/

    (toggle capturing on/off)
The DOM Event Object
function handleClick(e) {

    alert(e.type); // click

    alert(e.target); // element clicked

    alert(e.currentTarget); //element this handler was attached to

    e.preventDefault(); // don’t perform the default browser action

    e.stopPropagation(); // stop this event from capturing/bubbling

}
The DOM Event Object

    http://jsfiddle.net/pfrueh/sAGrq/

    (target, currentTarget, preventDefault,
stopPropagation, and toggling on/of capturing)
A Note About
          The IE Event Object (Pre-IE9)
function handleClick() { //  note: event just exists; it is not passed in

    alert(event.type); // == e.type

    alert(event.srcElement); // == e.target

    /* No equivalent of e.currentTarget */

    event.returnValue = false; // == e.preventDefault()

    event.cancelBubble = true; // == e.stopPropagation()

}
Event Delegation
In a nutshell: using one event handler on an
ancestor element to manage the interaction of
its descendant elements.

                    DEMO:
       http://www.linkedin.com/signal/
             (search for 'youtube')
Event Delegation

http://jsfiddle.net/pfrueh/b6vhS/
Thank You!

 Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Html events with javascript
Html events with javascriptHtml events with javascript
Html events with javascriptYounusS2
 
Quick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileQuick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileJussi Pohjolainen
 
jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');mikehostetler
 

Was ist angesagt? (6)

Events
EventsEvents
Events
 
Html events with javascript
Html events with javascriptHtml events with javascript
Html events with javascript
 
Quick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileQuick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery Mobile
 
WordPress Multisite
WordPress MultisiteWordPress Multisite
WordPress Multisite
 
jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');
 
jQuery Behaviours
jQuery BehavioursjQuery Behaviours
jQuery Behaviours
 

Andere mochten auch

Big Data and Data Standardization at LinkedIn
Big Data and Data Standardization at LinkedInBig Data and Data Standardization at LinkedIn
Big Data and Data Standardization at LinkedInAlexis Baird
 
Structural organization and architecture of a virtual reality explorer
Structural organization and architecture of a virtual reality explorerStructural organization and architecture of a virtual reality explorer
Structural organization and architecture of a virtual reality explorerPrachi Gupta
 
Data Mashups -Data Science Summit
Data Mashups -Data Science SummitData Mashups -Data Science Summit
Data Mashups -Data Science SummitPeter Skomoroch
 
Practical Problem Solving with Data - Onlab Data Conference, Tokyo
Practical Problem Solving with Data - Onlab Data Conference, TokyoPractical Problem Solving with Data - Onlab Data Conference, Tokyo
Practical Problem Solving with Data - Onlab Data Conference, TokyoPeter Skomoroch
 
Street Fighting Data Science
Street Fighting Data ScienceStreet Fighting Data Science
Street Fighting Data SciencePeter Skomoroch
 
Developing Data Products
Developing Data ProductsDeveloping Data Products
Developing Data ProductsPeter Skomoroch
 
Geo Analytics Tutorial - Where 2.0 2011
Geo Analytics Tutorial - Where 2.0 2011Geo Analytics Tutorial - Where 2.0 2011
Geo Analytics Tutorial - Where 2.0 2011Peter Skomoroch
 
Introducing LinkedIn Endorsements
Introducing LinkedIn EndorsementsIntroducing LinkedIn Endorsements
Introducing LinkedIn EndorsementsLinkedIn
 
Winning Talent on Mobile | Talent Connect Vegas 2013
Winning Talent on Mobile | Talent Connect Vegas 2013Winning Talent on Mobile | Talent Connect Vegas 2013
Winning Talent on Mobile | Talent Connect Vegas 2013LinkedIn Talent Solutions
 

Andere mochten auch (9)

Big Data and Data Standardization at LinkedIn
Big Data and Data Standardization at LinkedInBig Data and Data Standardization at LinkedIn
Big Data and Data Standardization at LinkedIn
 
Structural organization and architecture of a virtual reality explorer
Structural organization and architecture of a virtual reality explorerStructural organization and architecture of a virtual reality explorer
Structural organization and architecture of a virtual reality explorer
 
Data Mashups -Data Science Summit
Data Mashups -Data Science SummitData Mashups -Data Science Summit
Data Mashups -Data Science Summit
 
Practical Problem Solving with Data - Onlab Data Conference, Tokyo
Practical Problem Solving with Data - Onlab Data Conference, TokyoPractical Problem Solving with Data - Onlab Data Conference, Tokyo
Practical Problem Solving with Data - Onlab Data Conference, Tokyo
 
Street Fighting Data Science
Street Fighting Data ScienceStreet Fighting Data Science
Street Fighting Data Science
 
Developing Data Products
Developing Data ProductsDeveloping Data Products
Developing Data Products
 
Geo Analytics Tutorial - Where 2.0 2011
Geo Analytics Tutorial - Where 2.0 2011Geo Analytics Tutorial - Where 2.0 2011
Geo Analytics Tutorial - Where 2.0 2011
 
Introducing LinkedIn Endorsements
Introducing LinkedIn EndorsementsIntroducing LinkedIn Endorsements
Introducing LinkedIn Endorsements
 
Winning Talent on Mobile | Talent Connect Vegas 2013
Winning Talent on Mobile | Talent Connect Vegas 2013Winning Talent on Mobile | Talent Connect Vegas 2013
Winning Talent on Mobile | Talent Connect Vegas 2013
 

Ähnlich wie DOM Events

JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom ManipulationMohammed Arif
 
Modern frontend development with VueJs
Modern frontend development with VueJsModern frontend development with VueJs
Modern frontend development with VueJsTudor Barbu
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Wilson Su
 
jQuery : Events are where it happens!
jQuery : Events are where it happens!jQuery : Events are where it happens!
jQuery : Events are where it happens!Wildan Maulana
 
5 .java script events
5 .java script   events5 .java script   events
5 .java script eventschauhankapil
 
types of events in JS
types of events in JS types of events in JS
types of events in JS chauhankapil
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événementsJean Michel
 
WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)Prashanth Shivakumar
 
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] Ayes Chinmay
 
Session4 J2ME Mobile Information Device Profile(MIDP) Events
Session4 J2ME Mobile Information Device Profile(MIDP) EventsSession4 J2ME Mobile Information Device Profile(MIDP) Events
Session4 J2ME Mobile Information Device Profile(MIDP) Eventsmuthusvm
 
HTML5 Web Messaging
HTML5 Web MessagingHTML5 Web Messaging
HTML5 Web MessagingMike Taylor
 

Ähnlich wie DOM Events (20)

Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 
Client Web
Client WebClient Web
Client Web
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Modern frontend development with VueJs
Modern frontend development with VueJsModern frontend development with VueJs
Modern frontend development with VueJs
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8
 
jQuery : Events are where it happens!
jQuery : Events are where it happens!jQuery : Events are where it happens!
jQuery : Events are where it happens!
 
Lec 5
Lec 5Lec 5
Lec 5
 
5 .java script events
5 .java script   events5 .java script   events
5 .java script events
 
types of events in JS
types of events in JS types of events in JS
types of events in JS
 
JS basics
JS basicsJS basics
JS basics
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
 
Event
EventEvent
Event
 
WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)WPF (Windows Presentation Foundation Unit 01)
WPF (Windows Presentation Foundation Unit 01)
 
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]
 
Scripting The Dom
Scripting The DomScripting The Dom
Scripting The Dom
 
Session4 J2ME Mobile Information Device Profile(MIDP) Events
Session4 J2ME Mobile Information Device Profile(MIDP) EventsSession4 J2ME Mobile Information Device Profile(MIDP) Events
Session4 J2ME Mobile Information Device Profile(MIDP) Events
 
Windows Runtime Apps
Windows Runtime AppsWindows Runtime Apps
Windows Runtime Apps
 
HTML5 Web Messaging
HTML5 Web MessagingHTML5 Web Messaging
HTML5 Web Messaging
 

Kürzlich hochgeladen

Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...anilsa9823
 
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...Leko Durda
 
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual serviceanilsa9823
 
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service DhuleDhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhulesrsj9000
 
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual serviceanilsa9823
 
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...ur8mqw8e
 
Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666nishakur201
 
Postal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilisePostal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utiliseccsubcollector
 
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual serviceanilsa9823
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girlsPooja Nehwal
 
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female serviceanilsa9823
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,dollysharma2066
 
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改atducpo
 
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改atducpo
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭o8wvnojp
 
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonCheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonDelhi Call girls
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushShivain97
 
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfBreath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfJess Walker
 

Kürzlich hochgeladen (20)

Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
 
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...
Reinventing Corporate Philanthropy_ Strategies for Meaningful Impact by Leko ...
 
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Mahanagar Lucknow best sexual service
 
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service DhuleDhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
 
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Aliganj Lucknow best sexual service
 
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
 
Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666
 
Postal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilisePostal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilise
 
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
 
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Rajajipuram Lucknow best sexual service
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
 
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Adil Nagar Lucknow best Female service
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
 
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
 
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改
文凭办理《原版美国USU学位证书》犹他州立大学毕业证制作成绩单修改
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭
 
escort service sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
escort service  sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974escort service  sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
escort service sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
 
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonCheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by Mindbrush
 
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfBreath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
 

DOM Events

  • 3. The 1040 is a Document!
  • 4. google.com is a Document!
  • 5. yahoo.com is a Document!
  • 6. facebook.com is a Document!
  • 7. linkedin.com is a Document!
  • 8. Prerequisite Brainwashing • The WWW is made up of documents • Documents can contain images, forms, links to other documents, embedded media, etc. • No matter how dynamic, interactive, personalized, or app-like a web page is, it’s still just a document.
  • 9. Topics 1. The DOM is the BOM 2. DOM Events and How to (At/De)tach Them 3. DOM Event Flow 4. The Event Object 5. Event Delegation
  • 10. The Browser Object Model (BOM) The interface between the browser and JavaScript (starting at the top-level window object) PROPERTIES: window.navigator, window.location, window.frames, window.history, etc. METHODS: window.open, window.close, window.alert, etc. EVENTS: window.onscroll, window.onresize, etc.
  • 11. The Browser Object Model (BOM) http://jsfiddle.net/pfrueh/AXtfN/
  • 12. The Document Object Model (DOM) • “The DOM is a language- and platform neutral interface that allows programs and scripts to dynamically access and update the content and structure of documents.” • In the browser, the document object is created automatically with each new HTML document; it is assigned to the window object:
  • 13. The Document Object Model (DOM) In the DOM, everything’s a Node, and there are 12 subclasses of Node, the most common are: Document: window.document Element: myEl = document.getElementById('my-div') Attribute: element.setAttribute('href', 'http://www.linkedin.com') element.getAttribute('bar') // null if !exists on element myAttr = document.createAttribute('href') Text: myText = document.createTextNode('Hello World')
  • 14. The Document Object Model (DOM) http://jsfiddle.net/pfrueh/w4a7W/
  • 15. Events: HTML <!-- alerts a reference to the event object (discussed later) --> <a href="#" onclick="alert(event)">...</a> <!-- alerts a reference to the <a> element --> <a href="#" onclick="alert(this)">...</a> <!-- this used to be a common pattern --> <a href="#" onclick="handleClick(event, this)">...</a>
  • 16. Events: DOM Level 0 <div id="my-div">My Div</div> var myDiv = document.getElementById('my-div'); myDiv.onclick = function(e) { alert(e); // alerts the event object alert(this); // alerts a reference to myDiv }; // ^ what's the problem with this approach?
  • 17. Events: DOM Level 2 var myA = document.getElementById('my-a'); // W3C compliant browsers myA.addEventListener('click', handleClick, false); // IE less than 9 still requires proprietary events :( myA.attachEvent('onclick', handleClick);
  • 18. Simple Pattern For Modern Browsers (Using Native JS and not a Library) if (element.addEventListener) { //  feature/object detection element.addEventListener('click', clickHandler, false); } else if (element.attachEvent) { // Pre-IE9 element.attachEvent('onclick', clickHandler); } if (element.removeEventListener) { element.removeEventListener('click', clickHandler, false); } else if (element.detachEvent) { // Pre-IE9 element.detachEvent('onclick', clickHandler); }
  • 20. How to Tell When the DOM is Loaded document.addEventListener('DOMContentLoaded', init, false); // ^ works for all modern browsers including IE 9+ (but not before) http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/Default.html // libraries will take care of the pre-IE9 issue, so use them jQuery: $(document).ready(init) OR $(init) YUI 2: YAHOO.Event.onDOMReady(init) YUI 3: Y.on('domready', init)
  • 23. The DOM Event Object function handleClick(e) { alert(e.type); // click alert(e.target); // element clicked alert(e.currentTarget); //element this handler was attached to e.preventDefault(); // don’t perform the default browser action e.stopPropagation(); // stop this event from capturing/bubbling }
  • 24. The DOM Event Object http://jsfiddle.net/pfrueh/sAGrq/ (target, currentTarget, preventDefault, stopPropagation, and toggling on/of capturing)
  • 25. A Note About The IE Event Object (Pre-IE9) function handleClick() { //  note: event just exists; it is not passed in alert(event.type); // == e.type alert(event.srcElement); // == e.target /* No equivalent of e.currentTarget */ event.returnValue = false; // == e.preventDefault() event.cancelBubble = true; // == e.stopPropagation() }
  • 26. Event Delegation In a nutshell: using one event handler on an ancestor element to manage the interaction of its descendant elements. DEMO: http://www.linkedin.com/signal/ (search for 'youtube')

Hinweis der Redaktion

  1. Browser Object Model isn’t a standardized term, but it’s somewhat common.
  2. Most languages have a DOM implementation (read a file and then convert it into a DOM Document object)The original DOM Level 1 had a CORE and HTML moduleDOM 2 adds Events and a bunch of other modules
  3. The DOM also has collections of nodes
  4. Was common in web dev books until as late as ~2002-2004This is not the recommended way of attaching events, and if you do it on LinkedIn, your colleagues will never let you touch another line of JS ever again
  5. “DOM Level 0” refers to the browser manufacturer’s DOM, before the W3C got involved.