Forum Moderators: open
var thisObj = this;
// event handling
if (grandKids[j].addEventListener) {
grandKids[j].addEventListener('mouseover', function(e) {thisObj.show(e).call(thisObj)}, false);
grandKids[j].addEventListener('mouseout', function(e) {thisObj.hide().call(thisObj)}, false);
}
else if (grandKids[j].attachEvent) {
grandKids[j].attachEvent('onmouseover', function() {thisObj.show().call(thisObj)});
grandKids[j].attachEvent('onmouseout', function() {thisObj.hide().call(thisObj)});
}
else {
grandKids[j].onmouseover = thisObj.show().call(thisObj);
grandKids[j].onmouseout = thisObj.hide().call(thisObj);
}
Pass the event parameter "e" in 2nd case the same as 1st case ?
function show(e) {
if (!e) var e = window.event; // deal with IE
// function code
}
I didnt try it for the else case, although javascript doesnt set an event variable to be sent to the function. So while I will give it a try I cant see that it would work.
There are differences in the IE event object and a standard event object, do you have reason to think the error relates to the code you have shown as opposed to the internals of show and hide ?
What browser does the third case need to work for ( or was tested on ) ?
I tested the show and hide functions independently of the object and they worked, so I dont think that they are at fault.
show = function(e) {
if (!e) {var e = window.event;}
// Location of event
var posX = 0;
var posY = 0;
var targ = null;
targ = e.target ? e.target : e.srcElement;
if (e.pageX ¦¦ e.pageY) {
posX = e.pageX;
posY = e.pageY;
}
else if (e.clientX ¦¦ e.clientY) {
posX = e.clinetX;
posY = e.clientY;
posX += document.body.scrollLeft + document.documentElement.scrollLeft;
posY += document.body.scrollTop + document.documentElement.scrollTop;
}
// text for infoPopup
var targId = targ.getAttribute('id');
var re = new RegExp('^' + this.infoWindowId + '(\\d+)$');
// window.alert('re string: ' + re.toString());
targId = re.test(targId);
targId = RegExp.$1;
text = this.infoArray[targId];
this.createPopup(posX, posY, text);
}
The createPopup function is creating a div and textNode and a bit of styling, so it is unlikely to be that function that is at fault.
thisObj.show().call(thisObj)
my understanding is .call applies to a function, but thisObj.show() does not return a function ? If the object
is to just call show on thisObj then would the following suffice.
thisObj.show.call(thisObj)
or just
thisObj.show()
So the call method is their to make sure that the correct objects method is called. It should work without the call method, however it doesnt. I am at a loss as to why.
Their is a chance of this with the event handling function. As the first time the event occurs no error is generated, however the second time their is an error. However the call method results in the correct code getting executed.
I am beginning to think that I should have resisted my OOP impulses and written procedural code.
thisObj.show().call(thisObj)
is different from
thisObj.show.call(thisObj)
In the second case a method called show will execute with "this" set to thisObj
In the first case the method called will depend upon what is returned from executing thisObj.show(), it is equiv to:
var X = thisObj.show();
X.call(thisObj);
Your function call does not have a return statement, not sure what is returned by default.