Hi and thanks for looking at my post. I’m very new to scripting, and I’m having a simple problem that I can’t figure out. So…
I have a button that freezes up when I press it, and its “onclick” function doesn’t get triggered. There is a “onmouseout” function that is causing this, but I don’t know why.
I’d like “onmouseout” and “onclick” functions to apply to one button, but it’s not working. Please see my code:
Javascript:
function popup()
{
alert("Hello World")
}
function pop2()
{
alert("Good job")
}
function pop3()
{
alert("CLICK ME!")
}
HTML:
<input type="button" onclick="popup()" value="Hello World">
<input type="button" onclick="pop2()" onmouseout="pop3()" value="click me">
Adjusting your code to isolate and provide detail to what’s going on, I’ve prepared the following:
http://jsfiddle.net/238Nz/8/
The markup I am using to test your problem is the following:
Notice I plainly label my functions and use descriptive error messages. This helps me keep track of what is going on by embedding that detail within the code and the message log information. Always try to be descriptive in your labels (
function,var, log information, etc.), instead of doing things likeyay!orwhat happened. It’s simply too hard to follow once your scripts get to be longer and more complex.Next, I used the following function to narrow down and consistently replicate the same action:
This allows me to call both
console.logandalertin a specific order, from both event handlers. Next, I establish my two event handler functions:See, all they do is call
popup()with an action-specific log message about what was supposed to happen. Sincealerts are designed to “focus” to the user and block interaction, which appears to be a possible cause of the “frozen” browser frame.What I do next is try this in different browsers. Always try to replicate and test across different browsers#. In this case, I notice a big difference between two…
Chrome: Does not block processing; both
alerts fire, and theonmouseout-firedalertis not run throughconsole.loguntil after I hitOkon thealertand and I move the mouse pointer off of theinputelement. This, it seems, is the desired outcome. Right?Firefox (17.0.1): Firefox does show the behavior you’re describing. Note, when I click on the button, I get both
doclickanddomouseout()called at the same time. So Firefox is detecting theonclickas taking the mouse pointer away from the button, and you get the “freeze”. If you watch theconsole, you’ll see bothlogs fire immediately, and you seemingly get noalertto interact with (by clickingOk).IE (7-9, 9 Compatibility View): IE of course provides an interesting illustration. For instance, when I click the button in IE9, I see:
https://i.stack.imgur.com/Rzj82.png
Which of course appears to be the same effect Firefox is having… But for some reason with Firefox, the
onmouseout-firedalertdoes not focus on top of theonclick-firedalert. IE 7-9 plus Compatibility View all exhibit this particular behavior, with slight variations.Opera (12.02): Opera does not fire the
onmouseout-firedalertorconsole.logmessage until after theonclick-firedalertand log message, and you move the mouse (assuming you’ve moved it off of theinputbutton element after clicking it). This seems weird, but more palatable than the Firefox and IE behaviors. Maybe I’m mistaken, though.So what’s happening? I’m not quite sure, but I think that the
onmouseoutis blocking theonclick‘salertfrom focusing to the user. If you hit[Enter]while it’s frozen, you get theonclickalert but noonmouseout. Chrome seems correct here; Firefox’s “popunder” alert seems, well, sorta fishy.In summary, at least the behavior of the two events in this case are not only specific to Firefox. What seems to be specific to Firefox (at least 17.0.1) is the fact the
onmouseout-firedalertdoes not focus correctly, and the page “appears to freeze”. This seems like a bug. I wonder if it’s been reported?