1
Jay Urbain, Ph.D.
Dynamic HTML
- Handling (non-apocalyptic) events from
Jay Urbain, Ph.D. 1 Dynamic HTML is about scripting the DOM to - - PowerPoint PPT Presentation
Dynamic HTML Handling (non-apocalyptic) events from DOM objects Jay Urbain, Ph.D. 1 Dynamic HTML is about scripting the DOM to change a web page document after it is loaded into a browser But how do you know when the browser has finished
1
2
But how do you know when the browser has finished loading the document?
3
– Page load/unload – Offline/online – ….many more
– Button press – Mouse-over/click – Text entry – Input focus/blur – …many more
4
Window events (valid only in <body> elements)
Input element events
Mouse Events (validity restricted to certain elements)
Keyboard Events (validity restricted to certain elements)
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers http://www.w3schools.com/tags/ref_eventattributes.asp
5
<input type="button" id=goButton value="Push me!"
The onclick attribute specifies the JavaScript function that will be called when the button is pressed. The onClickHandler() method must have been defined or linked to from within the <head> section of the page. Note the ( ) following the method name! These are required!!!
6
The onclick attribute specifies the address of the JavaScript function that will be called when the button is pressed. Be sure to not supply “( )” after the function – otherwise, the result will be calling the function, rather assigning the function address. The script that calls getElementById must execute AFTER the page loads to make sure that the button element actually exists! Thus, getElementById is usually placed in the window.onload handler
<head> <script> //Code that executes after page loads: var button = getElementById(goButton); button.onclick = onClickHandler; // no () !!! </script> </head> <body> <input type="button" id=goButton value="Push me!"/>
<input type="button" name="b1" value="Create onclick=handleEvent(event);"
<input type="button" name="b2" value=Modify onclick=handleEvent(event);"
7
The window.event object can be passed as an argument to an event handler
8
9
var type = event.type; // works for both IE and FF parElement.innerHTML = "Type: " + type + " "; var eventSource; // Event sources have to be retrieved differently for IE and FF if( event.srcElement ) {// works for IE & Chrome eventSource = event.srcElement; parElement.innerHTML += "IE: "; } else { // works for FF eventSource = event.target; parElement.innerHTML += "FF: "; } Run demo3/Eventsdemo.html in se2840/demo3 project in WebStorm!
10
http://jsfiddle.net/pSS68/