Randy Webb wrote:
It falls in the same place the isIE variable falls. Its an utterly
flawed attempt to detect the browser when the browser doesn't matter.
Actually it falls into the category of 'I was trying to paraphrase the
sense of the code relating it to the OP's question, not give them a line
for line working solution'.
The only way that isMozilla/isIE would be accurate in that scenario is
if its defined like this:
if(div.addEventListener) isMozilla = true;
All of it is more predictable when written something like this:
if (div.addEventListener) {
div.addEventListener("click", clicked, false);
} else if (div.attachEvent) {
div.attachEvent("onclick", clicked);
}
Or possibly just check for one then assume the other. It depends whether it
is important to tell the user that the functionality they expect isn't
there, or if you just want to silently fail to provide the functionality.
Even then, div might not be a good variable name.
Nor would I expect things like 'click' to be hardwired in the final code.
Obviously proper code would have a general function for attaching events to
methods.
FWIW, the template I based the paraphrased version on is from Kupu. It also
has the broken method of testing for browser support, but it isn't so
significant in this case because Kupu explicitly only handles IE and
Mozilla (on any other browser it degrades to a textarea).
function addEventHandler(element, event, method, context) {
/* method to add an event handler for both IE and Mozilla */
var wrappedmethod = new ContextFixer(method, context);
try {
if (_SARISSA_IS_MOZ) {
element.addEventListener(event, wrappedmethod.execute, false);
} else if (_SARISSA_IS_IE) {
element.attachEvent("on" + event, wrappedmethod.execute);
} else {
throw "Unsupported browser!";
};
} catch(e) {
alert('exception ' + e.message + ' while registering an event
handler for element ' + element + ', event ' + event + ', method ' +
method);
};
};
function removeEventHandler(element, event, method) {
/* method to remove an event handler for both IE and Mozilla */
if (_SARISSA_IS_MOZ) {
window.removeEventListener('focus', method, false);
} else if (_SARISSA_IS_IE) {
element.detachEvent("on" + event, method);
} else {
throw "Unsupported browser!";
};
};
function ContextFixer(func, context) {
/* Make sure 'this' inside a method points to its class */
this.func = func;
this.context = context;
this.args = arguments;
var self = this;
this.execute = function() {
/* execute the method */
var args = new Array();
// the first arguments will be the extra ones of the class
for (var i=0; i < self.args.length - 2; i++) {
args.push(self.args[i + 2]);
};
// the last are the ones passed on to the execute method
for (var i=0; i < arguments.length; i++) {
args.push(arguments[i]);
};
self.func.apply(self.context, args);
};
};
An example of use matching the question could be:
Button.prototype.addClickHandler = function() {
var theDiv = document.getElementById(this.ID);
addEventHandler(theDiv, 'click', this.onClick, this);
}
Button.prototype.onClick = function() {
... whatever ...
}