Click to See Complete Forum and Search --> : Firefox doesn't support event object?


Zeb
February 2nd, 2005, 09:39 PM
I have a webpage which when a user puts the mouse over a picture it changes to something else, then back again - used for pretty buttons. Pretty standard.

It works perfectly in IE6, but I when I try testing it in Firefox, it doesn't work and the JavaScript console tells me "event is not defined". Now, I did some searching, and it appears that Firefox follows the DOM specification to the letter, while IE doesn't, and used some things that were in a draft version but not in the release version of the DOM spec. The weird thing is, I got the code for this from w3schools, which is done by w3 org which is the ones who (I thought) created DOM!

anyway, here is the code (relevant sections only):
<script language="javascript">
function highlight()
{
var sentence=event.srcElement.src
event.srcElement.src=sentence.substring(0, sentence.indexOf(".jpg")) + "sel.jpg"
}

function regular()
{
var sentence=event.srcElement.src
event.srcElement.src=sentence.substring(0, sentence.indexOf("sel.jpg")) + ".jpg"
}
</script>
..
..
..

<a href="home.htm" onmouseover="highlight()" onmouseout="regular()"><img src="img/home.jpg" border="0"></a>
Basically on mouseover I tell it to use the same jpg but with "sel" at the end of the name and then swap it back on mouseout.

how do i do this and support both IE and Firefox?

Dr. Script
February 2nd, 2005, 10:41 PM
Don't quote me on this, but have a param e, which I think is the error object. Then use:var o = ((typeof event != "undefined") ? (event) : (e)).srcElement;Replace the rest of the event.srcElements with o.

Again, I didn't research this, so I'm not positive. But this would require you to add the parameter e:function x{e} {Dr. Script

Zeb
February 2nd, 2005, 11:14 PM
i wish i could say that helped, but I can't get it to work :(

what am I passing into the function? i.e. I declare the function as "function x(e) {...}", but in the HTML event where I call it, what am I putting there? I tried putting the ID value (which I added but isn't in the example below), but that didn't work. not sure what else to try... it's such a common, simple thing...

Zeb
February 3rd, 2005, 12:12 AM
fixed it. moved the onmouseover and onmouseout events to the image, ditched the functions altogether and just say:
onmouseover="src='selected.jpg'"

and the reason i didn't try this earlier you ask? because the Visual studio .net 2003 ide told me there was no onmouseover/onmouseout event for an img tag.

Dr. Script
February 3rd, 2005, 06:42 AM
Glad you got it to work. If I had realized what you wante din the first place -- I was too tired -- you would have just sent an argument, this. Then you'd have a parameter e, and use: function highlight(e)
{
var sentence=e.src
e.src=sentence.substring(0, sentence.indexOf(".jpg")) + "sel.jpg"
} Of well, glad it works.

Dr. Script

Zeb
February 3rd, 2005, 04:40 PM
ahhh of course. i saw this in my wanderings and tried a few things, but never considered passing it as the argument. so, while in the end i found an easier way for this task, i know for the future when i need to do something more complicated.