dom events

28
DOM Events Pete Frueh

Upload: peter-frueh

Post on 30-Apr-2015

899 views

Category:

Self Improvement


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: DOM Events

DOM Events

Pete Frueh

Page 2: DOM Events

What’s a Document?

Page 3: DOM Events

The 1040 is a Document!

Page 4: DOM Events

google.com is a Document!

Page 5: DOM Events

yahoo.com is a Document!

Page 6: DOM Events

facebook.com is a Document!

Page 7: DOM Events

linkedin.com is a Document!

Page 8: DOM Events

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.

Page 9: DOM Events

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

Page 10: DOM Events

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.

Page 11: DOM Events

The Browser Object Model (BOM)

http://jsfiddle.net/pfrueh/AXtfN/

Page 12: DOM Events

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:

window.document

Page 13: DOM Events

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.documentElement: myEl = document.getElementById('my-div')Attribute:

element.setAttribute('href', 'http://www.linkedin.com')element.getAttribute('bar') // null if !exists on elementmyAttr = document.createAttribute('href')

Text: myText = document.createTextNode('Hello World')

Page 14: DOM Events

The Document Object Model (DOM)

http://jsfiddle.net/pfrueh/w4a7W/

Page 15: DOM Events

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>

Page 16: DOM Events

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?

Page 17: DOM Events

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);

Page 18: DOM Events

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); }

Page 19: DOM Events

Attaching Events

http://jsfiddle.net/pfrueh/EvXmW/

Page 20: DOM Events

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 themjQuery: $(document).ready(init) OR $(init)YUI 2: YAHOO.Event.onDOMReady(init)YUI 3: Y.on('domready', init)

Page 21: DOM Events

DOM Event Flow

Page 22: DOM Events

DOM Event Flow

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

(toggle capturing on/off)

Page 23: DOM Events

The DOM Event Objectfunction 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

}

Page 24: DOM Events

The DOM Event Object

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

(target, currentTarget, preventDefault, stopPropagation, and toggling on/of capturing)

Page 25: DOM Events

A Note AboutThe 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()

}

Page 26: DOM Events

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')

Page 28: DOM Events

Thank You!

Questions?