tags:

views:

76

answers:

2

There will sometimes be a lot of mouseover events and the $("*") selector may be expensive. Will this slow my pages to a crawl on slow machines running IE6?

Is there a better way to do this? I want to know about every mouseover event that happens on a page.

+2  A: 

I'm going to go out on a limb here and say that, judging by the way looking at that expression makes my teeth hurt, it may in fact be bad practice.

But if you absolutely must know about every mouseover ever, it may be the best thing available. I would, however, question that perceived need and hypothesize that the objectives you believe it will fulfill for you may be achieved in a superior fashion.

chaos
You're right that it's very bad. There's no need to assign a different handler to every element to know when every element has been moused over, however.
Rex M
+3  A: 

Just do $('body'). That will assign a single handler to the <body> element, and every descendant element (so, every element in the page) will bubble it's mouseover event up to that point. All you need to do inside the handler is check the originator of the event to find the exact element:

$('body').mouseover(function(e) {
    var sender = e.target;
    //sender is the element who was moused over
});

The key is not to do anything too intensive inside that handler, since it will basically be firing constantly as the user moves the mouse across your page. Best to start by checking the most restrictive conditions possible and return out of the method early as often as you can.

Rex M
+1, though note that it allows the child mouseover events to do `stopPropagation()` (which `live()` doesn't), so it may not fulfill the requirement of knowing about all mouseovers.
chaos
True. Maybe it's too much to assume that if the developer wants to react to every mouseover, s/he won't go around canceling them :)
Rex M
It wouldn't be so much the developer's own code that'd presumably be relevant as plugins and such.
chaos