JavaScript Event Handler Fail!!!

25 01 2010

My 1st attempt at writing a JavaScript event handler failed :( . I’m not sure what I did wrong, I’m going to have to ask my instructor, Gina, tomorrow in class. I’ll give you the basic rundown of what I was trying to do. In our last class, which was our introduction to event handlers we wrote a script that changed an unhappy pet rock


to a happy pet rock.

The script has an onload alert box that says, “hello, I am your pet rock.” Then it proceeds to have a prompt box that asks for your name, then the pet rock say, “Its good to meet you,” with your name inserted. We wrote the script for that in class. After it does all that, then rock changed from frown to smile. I thought I’d augment the script with a new alert box asking the user to scratch the rock, because he had an itch, then the pic would change the happy rock with a pop up saying, “aww, that feels good.” To accomplish this, I thought the best script to use would be the onMouseOver and onMouseOut event handlers, but no matter what I did it didn’t work.

At first all the alerts went by well, but the mouse events didn’t function, I had them between the body tags. From what I understand, when using onMouseOver you have to have it associated to a link and the script would look something like this a href=”#” onmouseover=”alert(‘awww, that feels good.’); return false” onmouseout=”document.getElementById(“rockIMG”).src =”rock_happy.png”. I moved this script to the header and the same thing happened…nothing, no mouse over events. oh the sadness! Then I did something, which I don’t quite remember what it was, and it worked too well. I had so many, “awww, that feels good” pop ups that I couldn’t close one without opening another (it was crazy!), finally, I had to force quite out of DreamWeaver.

Hopefully, tomorrow in class we’ll be able to figure out where I went wrong, correct it, and I’ll be able to post a link to a winner.

Advertisement

Actions

Information

3 responses

27 01 2010
jneal73

Thanks Dave, so my code was correct, there has to be something else going on. I think I’ll set up a gist.github.com account, and post the full scripts, cause I kept the script we did in class. If you can help me get all of it to work, I’d appreciate it!

26 01 2010
David Rose

Hmm. Nevermind. It actually worked for me in Google Chrome. code: http://gist.github.com/287146

26 01 2010
David Rose

I suspect it’s because the alert window steals focus from the element, so the element never fires an onMouseOut event.




Follow

Get every new post delivered to your Inbox.