26
Events DOM Event Model

Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Embed Size (px)

Citation preview

Page 1: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

EventsDOM Event Model

Page 2: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

HTML Events

<tag onevent="" />

"on" + event name

attribute value is javascript

javascript runs 1st

return false to STOP browser event (if any)

Page 3: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Layered Events

<p onclick="alert('p');"> <a onclick="alert('a');">click me</a> </p>

What happens? Why don't you try it?

<p onclick="alert('p');"> <a onclick="alert('a');return false;">click me</a> </p>

Why doesn't the return false stop it?

ONLY stops the <a> from going to href=""

Page 4: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

DOM Events

DOM API's event model - separate from HTML

SAME BASIC EVENT NAMES AND BEHAVIORS

no more "on" prefix!

many MORE events! create your own events!

Programs somewhat like JAVA's SWING API

More flexible and powerful (except in IE6)

Page 5: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

EventEvent Triggered WhenTriggered Whenclick clicked once (like :active)

mouseinThe mouse moves over an element

mouseoutThe mouse moves off an element

mouseover like :hovermousemove repeated event as fast cpumouseup button up - use this onemousedown button downscroll when scrollbars movekeydown key pressed downkeyup key was let up - use this one

Page 6: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

EventEvent Triggered WhenTriggered When

blur Element becomes inactive

select A user selects a field in a form

focusElement becomes active (anything tab-

able)

change The value of an element changes

submit A user submits a form

reset A form resets

resize window resize

load A document or image loads

unload A document unloads

abort The loading of an image is interrupted

error loading a document or image goes bad

Page 7: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

ElementElement EventEvent

<a> click, mouseover, mouseout

<img> about, error, load

<area> mouseover, mouseout

<body> blur, error, focus, load, unload

<frameset> blur, error, focus, load, unload

<frame> blur, focus

<form> submit, reset

<input type=”text”> blur, focus, change, select

<input type=”submit”> click

<input type=”reset”> click

<input type=”radio”> click

<input type=”checkbox”> blur, focus, change

<textarea> blur, focus, change, select

<select> blur, focus, change

OLD

Page 8: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

addEventListener()

HTMLElement.addEventListener(

"event name without on prefix. ex: click",

function object which is .call(this, event),

optional: true - capture event early

);

Page 9: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

HTML is easier.Why use DOM?

HTML on"event" requires CHANGES TO HTML

Automation, productivity, bandwidth

Flexibility

getElementsByClassName()

code reuse! - programming costs more

More features; + invalid HTML events can exist in DOM (some events only exist in DOM)

Page 10: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

function hi(){ alert("hello!"); }

document.body.addEventListener( 'load', hi );

// after page is loads// an alert window says "hello!"

Page 11: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

removeEventListener()

dispatchEvent()

fake events; trigger made up events

requires Event object:

e= document.createEvent("name")

e.initEvent(...) - setup properties

see also...

Page 12: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

HTML tags have a hierarchy parent tags can overlap events

<p onclick="alert('p')"> paragraph is parent

<a onclick="alert('a')">child tag</a>

</p>

Try it. experiment.

capture / bubble

Page 13: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

bodybody

pp

aaaa

Click!Click!

Bubble

(fa

lse)

Bubble

(fa

lse)

<a>'s onclick="runs"<a>'s onclick="runs"<a>'s href is followed<a>'s href is followed

Page 14: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

function capture(){ alert("captured!"); }function bubble(){ alert("bubbled"); }

tagObject= document.getElementById('id');

tagObject.addEventListener( 'click', bubble, false );tagObject.addEventListener( 'click', capture, true );

// A click on tagObject:// 1st says "hello!" (capture phase)// 2nd says "bye" (bubble phase - default)// 3rd tagObject does whatever it normally does//- likely nothing unless it is <a>, <button>, etc.

Page 15: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Demo

Page 16: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Common Use

Bubble only - HTML onEvents are bubble

Usually Bubble is all you want

MSIE never supported capture

Compatibility libraries forced to do same

So, addEventListener('event', function);

false is redundant, undefined comes out false

Page 17: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Event object

Describes details of the event; timestamp, keys

<a onclick="your_f(event)">click</a>

HTML onEvents set var event for you

You never work with Event, you use event an instance of Event

Page 18: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

tagObject.addEventListener('click', your_f, false)

what it does: your_f.call( tagObject, event)

function your_f(e){ alert(this); alert(e); }

FYI: Function.call(this, parameters);

your_f.call( this=object, parameters,... );

event use in DOM

Page 19: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Useful event methods

.preventDefault()

stops HTML tags from seeing event

function your_f(e){ e.preventDefault(); }

similar to HTML's <a onclick="return false;">

.stopPropagation()

event stops traveling down/up to other tags

Page 20: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Useful event Properties

.target = same as this; the object of the event

.keyCode, charCode = character pressed

.shiftKey, .ctrlKey, .altKey = true/false if down

.button = mouse button # down (only 1 at a time)

.clientX, .clientY, .screenX, .screenY = mouse

.bubbles = event is bubbling up

Page 21: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

variable "binding"

Problem: event handler function lack parameters

onclick="your_f(anything, I, want...)"

addEventListener( 'click', your_f)

nothing you want can be given to your_f

Page 22: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Solution 1

Javascript can hack any object

tagObject.addEventListener('click', your_f);

tagObject.myInformation= "hello";

function your_f(e) {

alert(this.myInformation);

alert(e.target.myInformation); }

Page 23: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Solution: exploit Javascript's memory management

function f(){

var x=5;

setTimeout( function(){alert(x);},1000);

} f();

variables actually DIE when nothing uses them

Solution 2:variable "binding"

Page 24: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

variable "binding"(see Closure)

Variables are references to memory storage

memory storage is freed when there are no more references

If you ADD references to the storage it will live until all of them are gone

javascript event handling HEAVILY uses "binding"

Page 25: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

function attach_an_event(){ var myInformation= "hello";

var your_f= function(e){ alert(myInformation);}tagObject.addEventListener('click', your_f);

}

// myInformation is "bound" to your_f// it lives until your_f dies and that will not die// if you removedEventListener then it would die// if you removed tagObject then it would die

Page 26: Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

function your_f(your_evt, your_info){alert(this);alert(your_evt.type);alert(your_info);

}

function bind( tag, evt, f, info ){var middleman= function(event){

return f.call(event.target, event, info);};tag.addEventListener(evt, middleman);

}

bind(document.body, "click", your_f, "hello");