1 / 56

CHAPTER 6 EVENTS

CHAPTER 6 EVENTS. WHAT IS AN EVENT?. Events are the browser’s way of saying, “Hey, this just happened.”. When an event fires , your script can then react by running code (e.g. a function).

lulaadams
Download Presentation

CHAPTER 6 EVENTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 6EVENTS

  2. WHAT IS AN EVENT?

  3. Events are the browser’s way of saying, “Hey, this just happened.”

  4. When an event fires, your script can then react by running code (e.g. a function).

  5. By running code when an event fires, your website responds to the user’s actions. It becomes interactive.

  6. DIFFERENT EVENT TYPES

  7. loadunloaderrorresizescroll USER INTERFACE EVENTS

  8. keydownkeyupkeypress KEYBOARD EVENTS

  9. clickdblclickmousedownmouseupmouseovermouseout MOUSE EVENTS

  10. focus / focusinblur / focusout FOCUS EVENTS

  11. inputchangesubmitresetcutcopypasteselect FORM EVENTS

  12. HOW EVENTS TRIGGER JAVASCRIPT CODE

  13. 1

  14. 1 Select the element node(s) the script should respond to

  15. 2 1 Select the element node(s) the script should respond to

  16. 2 1 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response

  17. 3 1 2 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response

  18. 1 2 3 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response State the code you want to run when the event occurs

  19. BINDING AN EVENT TO AN ELEMENT

  20. There are three ways to bind an event to an element:HTML event handler attributesTraditional DOM event handlersDOM Level 2 event listeners

  21. The following examples show a blur event on an element stored in a variable called el that triggers a function called checkUsername().

  22. <input type=“text” id=“username” onblur=“checkUsername()”> HTML EVENT HANDLER ATTRIBUTES(DO NOT USE)

  23. ELEMENT <input type=“text” id=“username” onblur=“checkUsername()”> HTML EVENT HANDLER ATTRIBUTES(DO NOT USE)

  24. <input type=“text” id=“username” onblur=“checkUsername()”> HTML EVENT HANDLER ATTRIBUTES(DO NOT USE) EVENT

  25. <input type=“text” id=“username” onblur=“checkUsername()”> HTML EVENT HANDLER ATTRIBUTES(DO NOT USE) FUNCTION

  26. el.onblur = checkUsername(); TRADITIONAL DOM EVENT HANDLERS

  27. el.onblur = checkUsername(); TRADITIONAL DOM EVENT HANDLERS ELEMENT

  28. el.onblur = checkUsername(); TRADITIONAL DOM EVENT HANDLERS EVENT

  29. el.onblur = checkUsername(); TRADITIONAL DOM EVENT HANDLERS FUNCTION

  30. EVENT LISTENERS el.addEventListener(‘blur’, checkUsername, false);

  31. EVENT LISTENERS el.addEventListener(‘blur’, checkUsername, false); ELEMENT

  32. EVENT LISTENERS el.addEventListener(‘blur’, checkUsername, false); EVENT

  33. EVENT LISTENERS el.addEventListener(‘blur’, checkUsername, false); FUNCTION

  34. EVENT LISTENERS el.addEventListener(‘blur’, checkUsername, false); BOOLEAN (OPTIONAL)

  35. Because you cannot have parentheses after the function names in event handlers or listeners, passing arguments requires a workaround.

  36. el.addEventListener(‘blur’, function() { checkUsername(5); }, false); PARAMETERS WITH EVENT LISTENERS

  37. el.addEventListener(‘blur’, function() { checkUsername(5); }, false); PARAMETERS WITH EVENT LISTENERS An anonymous function is used as the second argument.

  38. el.addEventListener(‘blur’, function() { checkUsername(5); }, false); PARAMETERS WITH EVENT LISTENERS Inside the anonymous function, a named function is called.

  39. IE5 - 8 had a different event model and did not support addEventListener() but you can provide fallback code to make event listeners work with older versions of IE.

  40. if (el.addEventListener) { el.addEventListener(‘blur’, function() { checkUsername(5); }, false); } else { el.attachEvent(‘onblur’, function() { checkUsername(5); }); } SUPPORTING OLDER VERSIONS OF IE

  41. if (el.addEventListener) { el.addEventListener(‘blur’, function() { checkUsername(5); }, false); } else { el.attachEvent(‘onblur’, function() { checkUsername(5); }); } SUPPORTING OLDER VERSIONS OF IE

  42. EVENT FLOW

  43. HTML elements nest inside other elements. If you hover or click on a link, you will also be hovering or clicking on its parent elements.

  44. EVENT BUBBLING

  45. EVENT CAPTURING

  46. THE EVENT OBJECT

  47. When an event occurs,the event object can tell you information about it and which element it happened upon.

  48. targettypecancelable PROPERTIES METHODS preventDefault() stopPropagation()

  49. 1: EVENT LISTENER CALLS FUNCTION function checkUsername(e) { var target = e.target; } var el = document.getElementById(‘username’); el.addEventListener(‘blur’, checkUsername, false); ELEMENT AN EVENT OCCURRED ON

More Related