{"id":594,"date":"2013-10-22T17:33:37","date_gmt":"2013-10-22T12:03:37","guid":{"rendered":"http:\/\/www.prasadk.com\/my-press\/?p=594"},"modified":"2013-10-23T17:32:58","modified_gmt":"2013-10-23T12:02:58","slug":"event-handlers-in-javascript","status":"publish","type":"post","link":"https:\/\/www.prasadk.com\/my-press\/event-handlers-in-javascript\/","title":{"rendered":"Understanding event handlers in JavaScript"},"content":{"rendered":"<h2>Understanding event handlers in JavaScript<\/h2>\n<p align=\"left\">What is event handlers? Its very powerful and useful;<\/p>\n<p align=\"left\">They are\u00a0<em>JavaScript code that are not added inside the &lt;script&gt; tags, but rather, inside the html tags,\u00a0<\/em>that execute JavaScript when something is triggered or called, such as pressing a button, moving your mouse over a link, submitting a form and so on.<\/p>\n<p align=\"left\">The basic syntax of these event handlers is:<\/p>\n<p align=\"left\">name_of_handler=&#8221;JavaScript code here&#8221;<\/p>\n<p align=\"left\">For example:<\/p>\n<div align=\"left\">\n<pre>&lt;a href=\"http:\/\/www.prasadk.com\" onClick=\"alert('Hi')\"&gt;prasadk.com Home&lt;\/a&gt;<\/pre>\n<p>Click here to have a look <a onclick=\"alert('Hi')\" href=\"http:\/\/www.prasadk.com\">prasadk.com Home<\/a><\/p>\n<\/div>\n<p>This is certainly unlike a regular JavaScript code, here we are inserting it directly inside a HTML tag, via the onClick event handler. When the above link is clicked, the user will first see an alert message before being taken to home \/ index page of http:\/\/www.prasadk.com.<\/p>\n<p>Different event handlers with with different HTML tags. For example, while &#8220;onclick&#8221; can be inserted into most HTML tags to respond to that tag&#8217;s onclick action, something like &#8220;onload&#8221; only works inside the &lt;body&gt; and &lt;img&gt; tags.<\/p>\n<p>Listed some of the most commonly used event handlers supported by JavaScript below:<\/p>\n<h4>\u00a0Event Handlers:<\/h4>\n<div align=\"left\">\n<table style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td style=\"background: #CBE86B;\"><strong>onclick:<\/strong><\/td>\n<td width=\"4\"><\/td>\n<td style=\"background: #F2E9E1;\">Use this to invoke JavaScript on clicking (a link, or form boxes)<\/td>\n<\/tr>\n<tr>\n<td style=\"background: #CBE86B;\"><strong>onload:<\/strong><\/td>\n<td width=\"4\"><\/td>\n<td style=\"background: #F2E9E1;\">Use this to invoke JavaScript after the page or an image has finished loading.<\/td>\n<\/tr>\n<tr>\n<td style=\"background: #CBE86B;\"><strong>onmouseover:<\/strong><\/td>\n<td width=\"4\"><\/td>\n<td style=\"background: #F2E9E1;\">Use this to invoke JavaScript if the mouse has been hovered on some link<\/td>\n<\/tr>\n<tr>\n<td style=\"background: #CBE86B;\"><strong>onmouseout:<\/strong><\/td>\n<td width=\"4\"><\/td>\n<td style=\"background: #F2E9E1;\">Use this to invoke JavaScript if the mouse goes pass some link or gets triggered when mouse is taken out of link<\/td>\n<\/tr>\n<tr>\n<td style=\"background: #CBE86B;\"><strong>onunload:<\/strong><\/td>\n<td width=\"4\"><\/td>\n<td style=\"background: #F2E9E1;\">Use this to invoke JavaScript right after someone leaves the page.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>In OOP \/\u00a0Object Oriented Event Handlers in Javascript<\/h4>\n<p>By default,\u00a0<b>when an event occurs<\/b>\u00a0in javascript, the\u00a0this\u00a0reference in the handler function\u00a0<b>refers to the HTML element<\/b>.<br \/>\nBut what if we want the &#8216;this&#8217; to refer to the class that houses our handler? We can use the\u00a0bind()function &#8211; see below.<\/p>\n<p><b>It works like this \/ snippet<\/b><\/p>\n<pre><code>\r\nFunction.prototype.bind = function (obj) {\r\n    var fn = this;\r\n    return function () {\r\n      var args = [this];\r\n\r\n      for (var i = 0, ix = arguments.length; i &lt; ix; i++) {\r\n        args.push(arguments[i]);\r\n      }\r\n      return fn.apply(obj, args);\r\n    }\r\n  }\r\n\r\n  function myClass() {\r\n    this.name = \"Member variable of myClass()!!\";\r\n\r\n    myClass.prototype.alertThis = function(el, e) {\r\n      if (!e) var e = window.event;\r\n      alert(this.name);   \/\/we have access to our class object\r\n      alert(\"id: \" + el.id);       \/\/we have access to the element that was clicked\r\n      alert(\"id: \" + e.target.id); \/\/we have access to our event\r\n    }.bind(this);\r\n\r\n    var el = document.getElementById(\"myDiv\");\r\n    el.onclick=this.alertThis;\r\n  }\r\n\r\n  function onLoad() {\r\n    window.c = new myClass();\r\n  }\r\n\r\n  if (window.addEventListener){\r\n    window.addEventListener('load', onLoad, false); \r\n  } else if (window.attachEvent){\r\n    window.attachEvent('onload', onLoad);\r\n  }\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Touch Event handlers for touch screen devices :<\/h3>\n<p>Some more event handlers are like\u00a0<strong>touchstart,\u00a0touchmove,\u00a0touchend,\u00a0touchcancel, tap, taphold, swipe, swipeleft, swiperight<\/strong><\/p>\n<p>snippet given below<\/p>\n<pre>function touchHandler(event) {\r\n    var touch = event.changedTouches[0];\r\n\r\n    var simulatedEvent = document.createEvent(\"MouseEvent\");\r\n        simulatedEvent.initMouseEvent({\r\n        touchstart: \"mousedown\",\r\n        touchmove: \"mousemove\",\r\n        touchend: \"mouseup\"\r\n    }[event.type], true, true, window, 1,\r\n        touch.screenX, touch.screenY,\r\n        touch.clientX, touch.clientY, false,\r\n        false, false, false, 0, null);\r\n\r\n    touch.target.dispatchEvent(simulatedEvent);\r\n    event.preventDefault();\r\n}\r\n\r\nfunction init() {\r\n    document.addEventListener(\"touchstart\", touchHandler, true);\r\n    document.addEventListener(\"touchmove\", touchHandler, true);\r\n    document.addEventListener(\"touchend\", touchHandler, true);\r\n    document.addEventListener(\"touchcancel\", touchHandler, true);\r\n}\r\n$(document).ready(function(){\r\n  init();\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>PN : while handling the touch event don&#8217;t forget to include jQuery, jQuery UI and jQuery mobile from cdn<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Understanding event handlers in JavaScript What is event handlers? Its very powerful and useful; They are\u00a0JavaScript code that are not added inside the &lt;script&gt; tags, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/594"}],"collection":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/comments?post=594"}],"version-history":[{"count":13,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/594\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}