JS - New Event Handler


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>Simple Event Example</title>
  5. <script type="text/javascript">
  6.  
  7. function addEventHandler(oNode, sEvt, fFunc, bCaptures) {
  8. if (typeof (window.event) != "undefined") {
  9. oNode.attachEvent("on" + sEvt, fFunc);
  10. }
  11. else {
  12. oNode.addEventListener(sEvt, fFunc, bCaptures);
  13. }
  14. }
  15.  
  16. function onLinkClicked(e) {
  17. alert('You clicked the link');
  18. }
  19.  
  20. function setUpClickHandler() {
  21. addEventHandler(document.getElementById('clickLink'), "click", onLinkClicked, false);
  22. }
  23.  
  24. addEventHandler(window, "load", setUpClickHandler, false);
  25.  
  26. </script>
  27. </head>
  28. <body>
  29. <a href="#" title="click me" id="clickLink">Click Me!</a>
  30. </body>
  31. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.