{"id":1056,"date":"2010-07-23T20:14:14","date_gmt":"2010-07-23T14:14:14","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=1056"},"modified":"2025-04-30T12:24:22","modified_gmt":"2025-04-30T12:24:22","slug":"event-object-javascript-tutorial","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/event-object-javascript-tutorial\/","title":{"rendered":"Event Object :: JavaScript Tutorial"},"content":{"rendered":"<p>Hello All &#8230;<\/p>\n<p>Hope enjoying most enjoyable activity .i.e development \ud83d\ude42 i love it &#8230; really love it ..<\/p>\n<p>Today i will try to explain something related to javascript event object. By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.<\/p>\n<p>Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.<\/p>\n<p>Examples of events:<\/p>\n<ul>\n<li>A mouse click<\/li>\n<li>A web page or an image loading<\/li>\n<li>Mousing over a hot spot on the web page<\/li>\n<li>Selecting an input field in an HTML form<\/li>\n<li>Submitting an HTML form<\/li>\n<li>A keystroke<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> Events are normally used in combination with functions, and the function will not be executed before the event occurs!<\/p>\n<p>I will give you some example scripts for reading out event properties. There are very serious browser incompatibilities in this area.<img loading=\"lazy\" decoding=\"async\" class=\"alignright\" title=\"event handling\" src=\"http:\/\/www.permadi.com\/tutorial\/jsEventBubbling\/images\/eventcapandbubbling.gif\" alt=\"\" width=\"299\" height=\"223\" \/><\/p>\n<p>As soon as we want to read out information about the event, we\u2019re inundated by an immense amount of properties, most of which do not work in most browsers. See the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_compinfo.html\">Event compatibility tables<\/a> for a quick overview or the\u00a0<a href=\"http:\/\/www.quirksmode.org\/dom\/w3c_events.html\">W3C DOM Compatibility &#8211; Events<\/a> page for a complete overview.<\/p>\n<p>I am not going to give an alphabetical list of properties, since it wouldn\u2019t help a bit to make you understand all this \u2014 the situation is too confusing. Instead I\u2019ve written five scripts to ask five questions of the browser.<\/p>\n<ol>\n<li>What is the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html#type\">type<\/a> of the event?<\/li>\n<li>Which HTML element is the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html#target\">target<\/a> of the event?<\/li>\n<li>Which\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html#key\">key<\/a> was pressed during the event?<\/li>\n<li>Which\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html#button\">mouse button<\/a> was pressed during the event?<\/li>\n<li>What was the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html#position\">mouse position<\/a> during the event?<\/li>\n<\/ol>\n<p>Please note that in these scripts I\u2019ve been very strict in my object detection. I first create cross-browser event access, then I check if the browser supports each individual property before using it.<\/p>\n<h3 id=\"type\">What is the type of the event?<\/h3>\n<p>This is the only question with a true cross-browser answer: use the\u00a0<code>type<\/code>property to read out the type:<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nfunction doSomething(e)<br \/>\n{<br \/>\n   if (!e) var e = window.event; alert(e.type);<br \/>\n}<br \/>\n[\/code]<\/p>\n<h3 id=\"target\">Which HTML element is the target of the event?<\/h3>\n<p>W3C\/Netscape says: the\u00a0<code>target<\/code>. No, says Microsoft, the\u00a0<code>srcElement<\/code>. Both properties return the HTML element the event took place on.<br \/>\n[code language=&#8221;javascript&#8221;]<br \/>\nfunction doSomething(e) {<br \/>\n\tvar targ;<br \/>\n\tif (!e) var e = window.event;<br \/>\n\tif (e.target) targ = e.target;<br \/>\n\telse if (e.srcElement) targ = e.srcElement;<br \/>\n\tif (targ.nodeType == 3) \/\/ defeat Safari bug<br \/>\n\t\ttarg = targ.parentNode;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>The last two lines of code are especially for Safari. If an event takes place on an element that contains text, this\u00a0<em>text node<\/em>, and not the element, becomes the target of the event. Therefore we check if the target&#8217;s<code>nodeType<\/code> is 3, a text node. If it is we move to its parent node, the HTML element.<\/p>\n<p>Even if an event\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_order.html\">is captured or bubbles up<\/a>, the target\/srcElement always remains the element the event took place on.<\/p>\n<h4 id=\"link3\">Other targets<\/h4>\n<p>There are some more targeting properties. I discuss\u00a0<code>currentTarget<\/code> on the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_order.html\">Event order<\/a> page and\u00a0<code>relatedTarget<\/code>,\u00a0<code>fromElement<\/code> and\u00a0<code>toElement<\/code> on the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_mouse.html\">Mouse events<\/a> page.<\/p>\n<h3 id=\"key\">Which key has been pressed?<\/h3>\n<p>This one is relatively easy. First get the code of the key (a = 65) from the<code>keyCode<\/code> property.<\/p>\n<p>When you\u2019ve read out the key code, send it through the method<code>String.fromCharCode()<\/code> to obtain the actual key value, if necessary.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nfunction doSomething(e) {<br \/>\n\tvar code;<br \/>\n\tif (!e) var e = window.event;<br \/>\n\tif (e.keyCode) code = e.keyCode;<br \/>\n\telse if (e.which) code = e.which;<br \/>\n\tvar character = String.fromCharCode(code);<br \/>\n\talert(&#039;Character was &#039; + character);<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>There are some subtleties that may make the key events hard to use. For instance, the\u00a0<code>keypress<\/code> event fires as long as the user keeps the key pressed. However, in most browsers the\u00a0<code>keydown<\/code> event also keeps firing as long as the key is pressed. I\u2019m not sure this is a good idea, but it\u2019s what happens.<\/p>\n<h3 id=\"button\">Which mouse button has been clicked?<\/h3>\n<p>There are two properties for finding out which mouse button has been clicked:\u00a0<code>which<\/code> and\u00a0<code>button<\/code>. Please note that these properties don\u2019t always work on a\u00a0<code>click<\/code> event. To safely detect a mouse button you have to use the\u00a0<code>mousedown<\/code> or\u00a0<code>mouseup<\/code> events.<\/p>\n<p><code>which<\/code> is an old Netscape property. Left button gives a value of 1, middle button (mouse wheel) gives 2, right button gives 3. No problems, except its meager support (and the fact that it\u2019s also used for key detection).<\/p>\n<p>Now\u00a0<code>button<\/code> has been fouled up beyond all recognition. According to W3C its values should be:<\/p>\n<ul>\n<li>Left button \u2013 0<\/li>\n<li>Middle button \u2013 1<\/li>\n<li>Right button \u2013 2<\/li>\n<\/ul>\n<p>According to Microsoft its values should be:<\/p>\n<ul>\n<li>Left button \u2013 1<\/li>\n<li>Middle button \u2013 4<\/li>\n<li>Right button \u2013 2<\/li>\n<\/ul>\n<p>No doubt the Microsoft model is better than W3C\u2019s. 0 should mean \u201cno button pressed\u201d, anything else is illogical.<\/p>\n<p>Besides, only in the Microsoft model button values can be combined, so that 5 would mean \u201cleft and middle button\u201d. Not even Explorer 6 actually supports this yet, but in the W3C model such a combination is<em>theoretically impossible<\/em>: you can never know whether the left button was also clicked.<\/p>\n<p>In my opinion\u00a0<a title=\"DOM Level 2 Events specification\" href=\"http:\/\/www.w3.org\/TR\/2000\/REC-DOM-Level-2-Events-20001113\/events.html#Events-eventgroupings-mouseevents\">W3C<\/a> has made some serious mistakes in defining\u00a0<code>button<\/code>.<\/p>\n<h4 id=\"link6\">Right click<\/h4>\n<p>Fortunately you most often want to know if the right button has been clicked. Since W3C and Microsoft happen to agree on this one and give<code>button<\/code> a value of 2, you can still detect a right click.<\/p>\n<pre>function doSomething(e) {\r\n\tvar rightclick;\r\n\tif (!e) var e = window.event;\r\n\tif (e.which) rightclick = (e.which == 3);\r\n\telse if (e.button) rightclick = (e.button == 2);\r\n\talert('Rightclick: ' + rightclick); \/\/ true or false\r\n}<\/pre>\n<p>Please note that, although Macs have only one mouse button, Mozilla gives a Ctrl\u2013Click a\u00a0<code>button<\/code> value of 2, since Ctrl\u2013Click also brings up the context menu. iCab doesn\u2019t yet support mouse button properties at all and you cannot yet detect a right\u2013click in Opera.<\/p>\n<h3 id=\"position\">Mouse position<\/h3>\n<p>As to the mouse position, the situation is horrible. Although there are no less than six mouse coordinates property pairs, there is no reliable cross\u2013browser way to find the mouse coordinates relative to the document we need.<\/p>\n<p>These are the six property pairs \u2014 see also the\u00a0<a href=\"http:\/\/www.quirksmode.org\/js\/events_compinfo.html\">Event compatibility tables<\/a> or the\u00a0<a href=\"http:\/\/www.quirksmode.org\/dom\/w3c_events.html#mousepos\">W3C DOM Compatibility &#8211; Events<\/a> page.<\/p>\n<ol>\n<li>clientX,clientY<\/li>\n<li>layerX,layerY<\/li>\n<li>offsetX,offsetY<\/li>\n<li>pageX,pageY<\/li>\n<li>screenX,screenY<\/li>\n<li>x,y<\/li>\n<\/ol>\n<p>I explained the problem, W3C\u2019s vagueness and the use of\u00a0<code>pageX\/Y<\/code> and<code>clientX\/Y<\/code> in my slightly outdated\u00a0<a title=\"Mission impossible - mouse position\" href=\"http:\/\/evolt.org\/article\/Mission_Impossible_mouse_position\/17\/23335\/index.html\">Evolt article<\/a>.<\/p>\n<p>The\u00a0<code>screenX<\/code> and\u00a0<code>screenY<\/code> properties are the only ones that are completely cross\u2013browser compatible. They give the mouse position relative to the entire computer screen of the user. Unfortunately this information is completely useless: you never need to know the mouse position relative to the screen \u2014 well, maybe only if you want to place another window at the mouse position.<\/p>\n<p>The other three property pairs are unimportant. See the\u00a0<a href=\"http:\/\/www.quirksmode.org\/dom\/w3c_events.html#mousepos\">W3C DOM Compatibility &#8211; Events<\/a> page for a description.<\/p>\n<h4 id=\"link8\">Correct script<\/h4>\n<p>This is the correct script for detecting the mouse coordinates:<\/p>\n<pre>function doSomething(e) {\r\n\tvar posx = 0;\r\n\tvar posy = 0;\r\n\tif (!e) var e = window.event;\r\n\tif (e.pageX || e.pageY) \t{\r\n\t\tposx = e.pageX;\r\n\t\tposy = e.pageY;\r\n\t}\r\n\telse if (e.clientX || e.clientY) \t{\r\n\t\tposx = e.clientX + document.body.scrollLeft\r\n\t\t\t+ document.documentElement.scrollLeft;\r\n\t\tposy = e.clientY + document.body.scrollTop\r\n\t\t\t+ document.documentElement.scrollTop;\r\n\t}\r\n\t\/\/ posx and posy contain the mouse position relative to the document\r\n\t\/\/ Do something with this information\r\n}<\/pre>\n<p>happy development \ud83d\ude42 have nice time &#8230;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/s07.flagcounter.com\/count\/oAB\/bg=FFFFFF\/txt=000000\/border=FFFFFF\/columns=8\/maxflags=200\/viewers=0\/labels=1\/pageviews=1\/\" alt=\"free counters\" border=\"0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello All &#8230; Hope enjoying most enjoyable activity .i.e development \ud83d\ude42 i love it &#8230; really love it .. Today i will try to explain something related to javascript event object. By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5234,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[372,373,591,592],"class_list":["post-1056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-event-object","tag-event-properties","tag-javascript-tutorial","tag-javascripts"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":3,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":5240,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions\/5240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media\/5234"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}