Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Attatch a event function to an element

Hi.

Is there a way to attatch an event function to an element which may allready have an event attatched? The problem is, that I do not want to override any events which may be present on the element from before. All I want is to run my code after all other code on that event has finished.

Like the following (this doesn't work):

var event = element.onclick;
var func = function() { alert("Me"); }
element.onclick = event + func;

the event variable will contain the following:

function anonymous() {
... some code ...
}

so I've also made a function to strip away any text before the first occurrence of '{' + 1 and after the last occurence of '}' - 1, thus leaving me with the content of the function only. Then I tried:

var event = stripFunction(element.onclick.toString());
var func = "alert('Me');";
element.onclick = event + func;

This worked fine in FireFox, but not in IE. I also tried wrapping the event + func inside a "function() {" + event + func + "}", and even a function() { eval(event); eval(func); }. (The latter one gave me a "Not implemented" error in IE).

Does anybody know how I can ... open up the event function, and insert my code ... or any other more clever ideas?
[1337 byte] By [greffin] at [2007-11-11 10:29:16]