Introduction
JavaScript is an event-driven language and it is wonderful. However it is plagued by a problem that server-side languages are not. The JavaScript ‘standard’ is not very standard at all. The problem is that it is down to the developers of the web browsers to implement the JavaScript standard in the same way. Easy right? No, unfortunately the real world and an ideal world are two completely separate beasts.
In JavaScript a very common requirement is to be able to use addEventListener and as time goes on this task is becoming less and less hassle. However if you require your site to support older versions of that pesky Internet Explorer you need to use attachEvent. What a Hassle!
As I’m sure you know there a seemingly endless number of JavaScript Libraries out there that take the pain out of attaching events in JavaScript. However, as awesome as these libraries are, sometimes you don’t require all of the extra features that come packed in with them.
There are times when all you want to do is to add event listener but it’s such a pain in you know what writing code every time that checks for addEventListener / attachEvent and uses accordingly soI decided it would be a good idea to create a handler for this specific task that can be reused and therefore make life a little easier without the overheads of an entire JavaScript library.
First I will share with you the handler then I will show you how to utilize it in your code.
First you need to create a new JavaScript file. So in visual studio go to File > New > File (or ctrl + shift + N) and select JavaScript file from the list.
Then in that JavaScript file add the following code:
- window.addingEvent = function (event, target, method) {
- if (target.addEventListener) {
- target.addEventListener(event, method, false);
- } else if (target.attachEvent) {
- target.attachEvent("on" + event, method);
- }
- }
First you need to create an HTML page. If you are using visual studio you can create an Html page by going to File > New > File (or ctrl + shift + n). As with JavaScript files you can use a text editor such as notepad. If you go with this option then create a new file and add the following markup:
- < !DOCTYPE html>
- <html lang = "en"
- xmlns = "http://www.w3.org/1999/xhtml" >
- <head >
- <meta charset = "utf-8" / >
- <title > Testing Handler.js < /title>
- </head>
- <body >
- </body>
- </html>
Now between your body tags add the following mark-up:
- < div id = "buttondiv" >
- <button id = "btnTest" name = "testButton" > Click me! < /button>
- </div>
Then after your title tags, in the head tags add the following css in a set of style tags:
- < style type = "text/css" > #buttondiv {
- text - align: center;
- }
- #
- btnTest {
- margin: 0 auto;
- width: 50 % ;
- }
- </style>
Now we want to call our function to add the click event to our button. Just before the closing body tag, we need to add 2 script tags like the following:
- < script type = "text/javascript"
- src = "handler.js" >
- <script type = "text/javascript" >
- </script>
To start off we need to create a variable to represent our button, like so:
- var theEl = document.getElementById("btnTest");
Then we need to call our function, to do that we need to call window.addingEvent(). We need to add the parameters to it, we will be adding an alert on the click event of our button. Your function call should look like the following:
- window.addingEvent('click', theEl, function () {
- alert("Hello World");
- });
What do you do when you want to handle such a situation without a framework? I’d love to hear from you about your cross-browser issues and resolutions. Any suggestions for improvement?
Read more articles on JavaScript

Guest UserPosted Jun 23, 2016, 8:55 PM
good to learn this capability and difference in addEventListener & addEvent
Bhavik PatelPosted Apr 14, 2016, 9:33 PM
useful.. like to include in my project.
Michael GriffithsPosted Apr 14, 2016, 6:43 PM
Thank you all for the comments
Kuppurasu NagarajPosted Apr 14, 2016, 9:24 AM
Nice Sharing..
Vignesh ManiPosted Apr 14, 2016, 8:15 AM
Good One
Rahul Kumar SaxenaPosted Apr 14, 2016, 6:25 AM
Good Show
Debasis SahaPosted Apr 13, 2016, 1:38 PM
Nice one..
Michael GriffithsPosted Apr 13, 2016, 1:28 PM
Thanks
Mohammed IbrahimPosted Apr 13, 2016, 1:10 PM
nice