O
Owen Brunker
Below is my MouseOvers Class which is now working fine. It currently
implements the event handler by specifying the handler functions in the
attributes of the HTML Element. I suppose you would call it inline but I am
not sure this is the correct terminolgy.
I have tried to implement the event handlers by assigning them to the
element from within the addItem method without success. The problem is how
to determine which anchor fired the event handler and thus identify the
images to be swapped. The object 'this' doesn't seem to be in context when
the event handler is called. Obviously I would have to pass the id of the
anchor element to the object method addItem() as well as the id of the image
for it to work. In the version below, only the id of the image is passed.
Further down the page is the broken version of what I am trying.
Thanks in advance
Owen Brunker
=== CODE START Working Version ===================
// Included as part of the header of the HTML page.
function MouseOvers() {
this.ImagesIn = new Array();
this.ImagesOut = new Array();
this.addItem = addItem;
this.eventMouseOver = eventMouseOver;
this.eventMouseOut = eventMouseOut;
}
function addItem(idImg, ImageSrcIn, ImageSrcOut) {
var element;
this.ImagesIn[idImg] = ImageSrcIn;
this.ImagesOut[idImg] = ImageSrcOut;
}
function eventMouseOver(idImg) {
var element;
element = document.getElementById(idImg);
element.src = this.ImagesIn[idImg];
}
function eventMouseOut(idImg) {
var element;
element = document.getElementById(idImg);
element.src = this.ImagesOut[idImg];
}
// This function is called from the HTML body and implements the MouseOvers
class
function WritePage() {
document.MouseOverEvents = new MouseOvers();
document.write('<table width="100%" cellpadding="0" cellspacing="0"><tr>');
document.write('<td>');
document.write('<a href="anotherpage.html"
onmouseover="document.MouseOverEvents.eventMouseOver(\'img1\')"
onmouseout="document.MouseOverEvents.eventMouseOut(\'img1\')" title="Does
Something"><img id="img1" src="images/imagein.png"></a>');
document.MouseOverEvents.addItem('img1', 'images/imageover.png',
'images/imagein.png');
document.write('</td>');
document.write('</tr></table>');
}
=== CODE END ===================================
The object below is implemented in the same way the above object is
implemented. The only differences are that the id of the anchor element is
passed as well. Also the HTML is not supposed to need to worry about inline
event handlers.
=== CODE START Broken Version ==================
function EventHandlers(idAnchor, idImg, ImageSrcIn, ImageSrcOut) {
var element;
this.id = idImg;
this.ImagesIn = ImageSrcIn;
this.ImagesOut = ImageSrcOut;
this.eventMouseOver = eventMouseOver;
this.eventMouseOut = eventMouseOut;
element = document.getElementById(idAnchor);
if (element != null) {
element.onmouseover = this.eventMouseOver;
element.onmouseout = this.eventMouseOut;
}
}
function MouseOvers() {
this.EvtMgr = new Array();
this.addItem = addItem;
}
function addItem(idAnchor, idImg, ImageSrcIn, ImageSrcOut) {
this.EvtMgr['id'] = new EventHandlers(idAnchor, idImg, ImageSrcIn,
ImageSrcOut);
}
function eventMouseOver() {
var element;
element = document.getElementById(this.id);
element.src = this.ImagesIn;
}
function eventMouseOut() {
var element;
element = document.getElementById(this.id);
element.src = this.ImagesOut;
}
=== CODE END ===================================
implements the event handler by specifying the handler functions in the
attributes of the HTML Element. I suppose you would call it inline but I am
not sure this is the correct terminolgy.
I have tried to implement the event handlers by assigning them to the
element from within the addItem method without success. The problem is how
to determine which anchor fired the event handler and thus identify the
images to be swapped. The object 'this' doesn't seem to be in context when
the event handler is called. Obviously I would have to pass the id of the
anchor element to the object method addItem() as well as the id of the image
for it to work. In the version below, only the id of the image is passed.
Further down the page is the broken version of what I am trying.
Thanks in advance
Owen Brunker
=== CODE START Working Version ===================
// Included as part of the header of the HTML page.
function MouseOvers() {
this.ImagesIn = new Array();
this.ImagesOut = new Array();
this.addItem = addItem;
this.eventMouseOver = eventMouseOver;
this.eventMouseOut = eventMouseOut;
}
function addItem(idImg, ImageSrcIn, ImageSrcOut) {
var element;
this.ImagesIn[idImg] = ImageSrcIn;
this.ImagesOut[idImg] = ImageSrcOut;
}
function eventMouseOver(idImg) {
var element;
element = document.getElementById(idImg);
element.src = this.ImagesIn[idImg];
}
function eventMouseOut(idImg) {
var element;
element = document.getElementById(idImg);
element.src = this.ImagesOut[idImg];
}
// This function is called from the HTML body and implements the MouseOvers
class
function WritePage() {
document.MouseOverEvents = new MouseOvers();
document.write('<table width="100%" cellpadding="0" cellspacing="0"><tr>');
document.write('<td>');
document.write('<a href="anotherpage.html"
onmouseover="document.MouseOverEvents.eventMouseOver(\'img1\')"
onmouseout="document.MouseOverEvents.eventMouseOut(\'img1\')" title="Does
Something"><img id="img1" src="images/imagein.png"></a>');
document.MouseOverEvents.addItem('img1', 'images/imageover.png',
'images/imagein.png');
document.write('</td>');
document.write('</tr></table>');
}
=== CODE END ===================================
The object below is implemented in the same way the above object is
implemented. The only differences are that the id of the anchor element is
passed as well. Also the HTML is not supposed to need to worry about inline
event handlers.
=== CODE START Broken Version ==================
function EventHandlers(idAnchor, idImg, ImageSrcIn, ImageSrcOut) {
var element;
this.id = idImg;
this.ImagesIn = ImageSrcIn;
this.ImagesOut = ImageSrcOut;
this.eventMouseOver = eventMouseOver;
this.eventMouseOut = eventMouseOut;
element = document.getElementById(idAnchor);
if (element != null) {
element.onmouseover = this.eventMouseOver;
element.onmouseout = this.eventMouseOut;
}
}
function MouseOvers() {
this.EvtMgr = new Array();
this.addItem = addItem;
}
function addItem(idAnchor, idImg, ImageSrcIn, ImageSrcOut) {
this.EvtMgr['id'] = new EventHandlers(idAnchor, idImg, ImageSrcIn,
ImageSrcOut);
}
function eventMouseOver() {
var element;
element = document.getElementById(this.id);
element.src = this.ImagesIn;
}
function eventMouseOut() {
var element;
element = document.getElementById(this.id);
element.src = this.ImagesOut;
}
=== CODE END ===================================