function addListener(element, event, listener, bubble) { if(element.addEventListener) { if(typeof(bubble) == "undefined") bubble = false; element.addEventListener(event, listener, bubble); } else if(this.attachEvent) { element.attachEvent("on" + event, listener); } else { debug("Could not set up event listener"); } } function getSource(event) { if(event.target) { return event.target; } else if(event.srcElement) { return event.srcElement; } debug("Could not find event source: " + event); return undefined; } var body = document.getElementsByTagName("body").item(0); var div = document.createElement("div"); body.appendChild(div); var scopeTest = function(event) { var scope = "unknown"; if(this == window) { scope = "window"; } else if(this == arguments.callee) { scope = "callee"; } else { scope += " (" + this + ")"; if(this.nodeName) { scope += " {" + this.nodeName + "}" }; } var message = "Scope of callback to: "; var source = getSource(event); message += "{" + (source ? source.nodeName : "No Source") + "}"; debug(message + ": " + scope + " [" + this.userData + "]"); } var fileImage = new Image(); fileImage.userData = "file image user data"; addListener(fileImage, "load", scopeTest); fileImage.src = "../tree_view/file.png"; div.appendChild(document.createElement("a")); div.lastChild.userData = "a #1 user data"; addListener(div.lastChild, "click", scopeTest); div.lastChild.appendChild(fileImage); function wrapper(event) { var source = getSource(event); if(source) { arguments.callee.handler.apply(source, arguments) } else { arguments.callee.handler.apply(this, arguments) } } wrapper.handler = scopeTest; var folderImage = new Image(); folderImage.userData = "folder image user data"; addListener(folderImage, "load", wrapper); folderImage.src = "../tree_view/folder.png"; div.appendChild(document.createElement("a")); div.lastChild.userData = "a #2 user data"; addListener(div.lastChild, "click", wrapper); div.lastChild.appendChild(folderImage);