{"id":25,"date":"2007-09-29T23:55:50","date_gmt":"2007-09-29T22:55:50","guid":{"rendered":"http:\/\/jimblackler.com\/blog\/?p=25"},"modified":"2007-09-29T23:55:50","modified_gmt":"2007-09-29T22:55:50","slug":"safely-adding-events-to-dom-elements-in-javascript-for-ie-firefox-opera-and-safari","status":"publish","type":"post","link":"https:\/\/jimblackler.com\/?p=25","title":{"rendered":"Safely adding events to DOM elements in Javascript for IE, Firefox, Opera and Safari"},"content":{"rendered":"<p>Recently I looked into some reported problems with my word game site Qindar.net and the Safari browser. This was a bit easier for me since Apple released a Windows version of Safari (which, albeit arguably surplus to requirements, is actually a very nice, usable browser on Windows).I discovered that the technique I was using to work out which method of event attachment to use was flawed and was failing for Safari. So I refined it slightly to fix the problem.<\/p>\n<p>The problem is that Javascript on Firefox, Opera and Safari support the &#8220;W3C DOM Level 2 event binding mechanism&#8221;, which uses a function on DOM elements called addEventListener. Internet Explorer however uses a technique that was apparently from before that particular standard was drawn up, employing a function called attachEvent. In addition, the names of the events are different. For instance, IE uses events such as &#8220;onmousemove&#8221;, &#8220;onmouseup&#8221;, but the other browsers omit the &#8220;on&#8221; and name these events &#8220;mousemove&#8221; and &#8220;mouseup&#8221;.<\/p>\n<p>Curiously, Opera is the only browser to support both styles.<\/p>\n<p>The simplest and safest way of working out which one to use is simply to test for the existence of a function called addEventListener. I quite like this method because it works on the latest version of the big four browsers, and IE 6, without having to do any browser version probing.<\/p>\n<p>For instance, here is how to add focus and focus lost events to a page in a way that will work on all modern browsers:<\/p>\n<p>[Javascript]<br \/>\nif (window.addEventListener != null)<br \/>\n{ \/\/ Method for browsers that support addEventListener, e.g. Firefox, Opera, Safari<br \/>\nwindow.addEventListener(&#8220;focus&#8221;, FocusFunction, true);<br \/>\nwindow.addEventListener(&#8220;blur&#8221;, FocusLostFunction, true);<br \/>\n}<br \/>\nelse<br \/>\n{ \/\/ e.g. Internet Explorer (also would work on Opera)<br \/>\nwindow.attachEvent(&#8220;onfocus&#8221;, FocusFunction);<br \/>\ndocument.attachEvent(&#8220;onfocusout&#8221;, FocusLostFunction); \/\/focusout only works on document in IE<br \/>\n}<br \/>\n[\/Javascript]<\/p>\n<p>This is how to add mouse events:<\/p>\n<p>[Javascript]<br \/>\nif (document.addEventListener != null)<br \/>\n{ \/\/ e.g. Firefox, Opera, Safari<br \/>\ndocument.addEventListener(&#8220;mousemove&#8221;, MouseMoveFunction, true);<br \/>\ndocument.addEventListener(&#8220;mouseup&#8221;, MouseUpFunction, true);<br \/>\n}<br \/>\nelse<br \/>\n{ \/\/ e.g. Internet Explorer (also would work on Opera)<br \/>\ndocument.attachEvent(&#8220;onmousemove&#8221;, MouseMoveFunction);<br \/>\ndocument.attachEvent(&#8220;onmouseup&#8221;, MouseUpFunction);<br \/>\n}<br \/>\n[\/Javascript]<\/p>\n<p>To remove the mouse events, I recommend&#8230;<\/p>\n<p>[Javascript]<br \/>\nif (document.removeEventListener != null)<br \/>\n{ \/\/e.g. Firefox, Opera, Safari<br \/>\ndocument.removeEventListener(&#8220;mousemove&#8221;, MouseMoveFunction, true);<br \/>\ndocument.removeEventListener(&#8220;mouseup&#8221;, MouseUpFunction, true);<br \/>\n}<br \/>\nelse<br \/>\n{ \/\/e.g. Internet Explorer (also would work on Opera)<br \/>\ndocument.detachEvent(&#8220;onmousemove&#8221;, MouseMoveFunction);<br \/>\ndocument.detachEvent(&#8220;onmouseup&#8221;, MouseUpFunction);<br \/>\n}<br \/>\n[\/Javascript]<\/p>\n<p>I personally pray there comes a time when these kinds of workarounds are not required. In the mean time, this will have to do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I looked into some reported problems with my word game site Qindar.net and the Safari browser. This was a bit easier for me since Apple released a Windows version of Safari (which, albeit arguably surplus to requirements, is actually a very nice, usable browser on Windows).I discovered that the technique I was using to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-web-development"],"_links":{"self":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25"}],"version-history":[{"count":0,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}