Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the "onmouseover" event use "return true" to prevent default behavior?

I have been searching for this for some time, but haven't got any explanation.

For "onclick" and other events in javascript, the event handler returning false means "prevent default action". However, there is one exception for "onmouseover". For "onmouseover", returning true means "prevent default action".

Why is there such a weird exceptional case for "onmouseover"?

like image 408
Morgan Cheng Avatar asked Feb 02 '09 05:02

Morgan Cheng


2 Answers

Instead of using return false / true to prevent default event behaviour, use the default method / attribute on the event object:

elem.onmouseover = function(e) {
    if (!e) var e = window.event; // IE
    if(e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false; // IE
    }
}
like image 110
Luca Matteis Avatar answered Sep 18 '22 18:09

Luca Matteis


As far as I know, returning true to prevent the url of a link to show up in the status bar is an IE-specific (mis-)feature. And trying to understand why IE does things the way it does is often a lost cause - just live with it...

like image 29
Christoph Avatar answered Sep 20 '22 18:09

Christoph