Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

How to modify addEventListener code to work for IE8

I have the following code that works to get the ID of an element when clicked but I also need this to work in IE8 which doesn't support addEventListener so how can I get this to work in IE8 also (***without browser sniffing - please use feature detection instead)?
document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    if(target.id != "")
    {
        alert(target.id);
    }
     
}, false);

Open in new window

0
894359
Asked:
894359
  • 3
  • 2
1 Solution
 
Robert SchuttSoftware EngineerCommented:
Since you need IE8 specifically what you need to add is attachEvent. There are a lot of nice cross-browser scripts out there on the 'net that do a lot more like making sure the proper context is set, provide access to other properties (text in element, pressed key, mouse coordinates) but for now the following code should do (based on a fiddle from a previous question which I updated).
function addEvent(el, ev, fn, uc) {
    if (el.addEventListener) {
        el.addEventListener(ev, fn, uc);
    } else if (el.attachEvent) { // IE8-
        el.attachEvent('on'+ev, fn);
    }
}

addEvent(document, 'click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    if(target.id != "")
    {
        alert(target.id);
    }
    //var text = target.textContent || target.innerText;
}, false);

Open in new window

If you need more/better functionality there are a lot of great generic solutions for this:
* http://javascriptrules.com/2009/07/22/cross-browser-event-listener-with-design-patterns/
* http://blog.stchur.com/2007/04/26/optimize-your-javascript-cross-browser-code-through-branching/
* http://therealcrisp.xs4all.nl/upload/addEvent_dean.html

Also reading MDN about IE8 should clarify some possible issues.

Then if you're still craving more information, read addEvent() considered harmful but like swimming I wouldn't recommend this within 2 hours after eating ;-)
0
 
894359Author Commented:
What I want is code that listens for a click on an element and then reads the "ID" as the initial code does but also works with IE8 which the current code doesn't do. Ive looked at some of the articles on this but I don't know Javascript enough to to do this without a lot of trial and error.

Thanks!
0
 
Robert SchuttSoftware EngineerCommented:
I have tested the updated fiddle with IE8 through the url http://jsfiddle.net/robert_schutt/4pd4xuvs/2/embedded/result/ and it works the same as the previous fiddle on other browsers as far as I can tell. Can you be more specific about what and/or how it's not working for you, have you tried to make a page with this code and debug with F12 for example? Are you able to share a link to a page where it's not working for you?

I know this is not convincing per se but here is a screenshot showing my IE version and the alert working.
capture
0
 
894359Author Commented:
Hi Robert,
Let me test and I will get back to you.

Thanks,
MJ
0
 
894359Author Commented:
Thanks Robert!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now