Except where otherwise noted, the contents of this presentation are © Copyright 2007 Marty Stepp and are licensed under the Creative Commons Attribution 2.5 License.
<body> <button id="ok" onclick="okay();">Click me</button> ...
// called when OK button is clicked function okay() { var button = document.getElementById("ok"); button.style.color = "red"; }
<body>
<button id="ok">Click me</button>
...
window.onload = initializeBody; // global code // called when page loads; sets up all event handlers function initializeBody() { document.getElementById("ok").onclick = okay; } function okay() { this.style.color = "red"; }
window.onload
eventwindow.onload = name; // called when page loads; sets up all event handlers function name() { set up any necessary event handlers using the DOM }
window
is one of several global DOM objectsonload
event property represents the loading of the web page
window.onload = initializeBody; function initializeBody() { var name = document.getElementById("ok"); name.event = function; ... }
DOM objects for HTML elements have the following properties:
onmousedown
: user presses down mouse button on this elementonmouseup
: user releases mouse button on this element onclick
: user presses/releases mouse button on this elementondblclick
: user presses/releases mouse button twice on this elementonmouseover
: mouse cursor enters this element's boxonmouseout
: mouse cursor exits this element's boxonmousemove
: mouse cursor moves within this element's box
myElement.onmousemove = myFunction;
<div id="dare">Click me ... I dare you!</div>
function initializeBody() { document.getElementById("dare").onmousedown = colorIt; } function colorIt() { this.style.backgroundColor = "red"; }
this
(don't have to call document.getElementById
again)<div id="dare">Click me ... I dare you!</div>
function initializeBody() { var dareDiv = document.getElementById("dare"); dareDiv.onmousedown = colorIt; dareDiv.onmouseup = uncolorIt; } function colorIt() { this.style.backgroundColor = "red"; } function uncolorIt() { this.style.backgroundColor = "transparent"; }
function colorIt(event) { this.style.backgroundColor = "red"; this.innerHTML = "You clicked (" + event.screenX + ", " + event.screenY + ")"); }
type
: what kind of event, such as "click"
or "mousedown"
on
prefixclientX
, clientY
: coordinates from top/left of pagescreenX
, screenY
: coordinates from top/left of screenevent
as a parameter
window.event
offsetX
, offsetY
: coordinates from top/left of element
layerX
, layerY
properties insteadbutton
: which mouse button was pressed/released, if any
which
property insteadsrcElement
: element that fired the event
target
property insteadfunction handleClick(event) { event = standardizeEvent(event); ... } // Repairs various browser incompatibilities. function standardizeEvent(event) { var e = event || window.event; e.srcElement = e.srcElement || e.target; return e; }
One of the coolest features of Google Maps is the ability to drag the map to move it around. Write a program with a draggable map of Middle Earth using Javascript mouse event handlers. (See the background CSS properties from the end of the CSS slides.)
<div id="gum"><img src="gum.png" alt="gum" /> Double your pleasure, <em>double your fun</em>!</div>
function initializeBody() { document.getElementById("gum").ondblclick = doBorder; } function doBorder(event) { event = standardizeEvent(event); event.srcElement.style.border = "2px dashed blue"; }
div
intercepts events on anything inside itDOM objects for HTML elements have the following properties:
onkeydown
: user presses a key while this element has keyboard focusonkeyup
: user releases a key while this element has keyboard focusonkeypress
: user presses and releases a key while this element has keyboard focusonfocus
: this element gains keyboard focusonblur
: this element loses keyboard focuskeyCode
: ASCII value of key pressed
String.fromCharCode(event.keyCode)
altKey
: true
if Alt key is being heldctrlKey
: true
if Ctrl key is being heldshiftKey
: true
if Shift key is being heldWhich key event properties does your browser support?
// in window.onload handler:
document.getElementById("mytextbox").onkeypress = keyPress;
function keyPress(event) {
event = standardizeEvent(event);
if (event.keyCode == 13) {
// key code 13 means the user pressed Enter
do something;
}
}
these are supported by <input type="text">
, <textarea>
Write Javascript code so that a text input box will only accept numbers as input. Any other characters typed will be removed from the box immediately.
// script 1 window.onload = init1;
// script 2
window.onload = init2;
window.onload
window.onload
handler
addEventListener
// script 1
window.addEventListener("load", init1, false);
// script 2
window.addEventListener("load", init2, false);
addEventListener
achieves this
element.addEventListener("event", function, false);
attachEvent
instead