[Javascript] attachEvent and "this"

Julien Nadeau junado at junado.com
Fri Aug 12 09:56:51 CDT 2005


Hi,

I've been working on some sort of image bank system and I'm having a  
problem using the "this" keyword.

I have the following code:

addEvent(btns[i].getAttribute('id'), "click", showBqImg);

refers to the following function:
function addEvent(id, type, handler) {
     if (!document.getElementById(id)) {
        return false;
     } else {
         var el = document.getElementById(id);
         if (el.addEventListener) {
             el.addEventListener(type, handler, false);
         } else if (el.attachEvent) {
             el.attachEvent("on" + type, handler);
         } else {
             el.onclick = handler;
         }
     }
}

which I use to attach an even to a specific button. This works in all  
browser (attaching the event) and this is the function it refers to:

function showBqImg(e) {
     referer = this.getAttribute('dest');
     el.style.display = "block";

     if (e.pageX && e.pageY) {
         el.style.left = (e.pageX + 20) + 'px';
         el.style.top = (e.pageY - 8) + 'px';
     } else {
         el.style.left = window.event.clientX +  
document.body.scrollLeft + 20 + 'px';
         el.style.top = window.event.clientY +  
document.body.scrollTop - 8 + 'px';
     }
}

So here I get the value of the "dest" attribute (homemade) I use to  
specify where the selected item should be written once "Choisir" is  
clicked. The "this" object is correctly set in Safari / Firefox, but  
Internet Explorer seems to mess it up.

You can see a working example at http://www.chyz.qc.ca/banque_images/.

My question is, using unobstrusive event handlers, how can I get to  
use the "this" keyword in Internet Explorer 6 so that I can get the  
info contained in the "dest" attribute. Is there's a way?

Thank you,

Julien Nadeau
junado at junado.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20050812/2a2fd7bd/attachment.htm>


More information about the Javascript mailing list