addEventListener and attachEvent for IE8. Getting to that stage where your almost out of IE8's reach but it just keeps pulling you back in?

I too have had instances where I've tried to make the breakaway but have been pulled back. One such is trying to use addEventListener more, however if you are in the world of IE8 (all the way down to IE5 too) then you are in harsh luck. However we can create a nice simple check to fall back to attachEvent (IE5-8). Add the following to your Javascript:

[javascript] function addMyEvent(element,eventType,handler) { if (element.addEventListener) element.addEventListener (eventType,handler,false); else if (element.attachEvent) element.attachEvent ('on'+eventType,handler); } // which can be used like so: jeeves = document.getElementById('jeevesID'); addMyEvent(jeeves,'click',functionToRun); functionToRun = function(){ console.log('Damn you sir'); } [/javascript]

notice in the attachEvent fall back the need to add: 'on'+eventType. This is IE5-8's way of doing it e.g. onClick onHover etc. If anyone knows the reason why that was the case I'd love to know.

Monthly Archives: June 2014

addEventListener and attachEvent for IE8

addEventListener and attachEvent for IE8. Getting to that stage where your almost out of IE8’s reach but it just keeps pulling you back in? I too have had instances where I’ve tried to make the breakaway but have been pulled back. One such is trying to use addEventListener more, however if you are in the

Read more

HTML5 Battery API

The battery API that comes with HTML5 is a really cool new API that will be great to use. It allows you to check the battery level status and whether the battery is being charged. As with all new HTML5 it is best to check: caniuse.com – Battery API. It will determine if you can

Read more

Drupal – Load Block in node template

Load block in node template. This is something that I needed to do recently in a custom node template and the following code is how you achieve it. The name of the region you wish to use in your .info file: In your template.php file use the template_preprocess_node function: Now in your node template use

Read more