MooTools Tip: Event.stop
Events are a huge part of JavaScript because so much of our JavaScript is triggered by the user's actions. Quite frequently we will assign a listener to a link and immediately want the default click action stopped. For example:
$('myLink').addEvent('click',function(e) {
e.stop();
//do stuff
});
The above code works great but there's one small hole I sometimes run into. On rare occasion I feel the need to trigger an event on an element:
$('myLink').fireEvent('click');
The problem is that the Event object is not given when an event is triggered using Element.fireEvent. The way to prevent any problems is an easy if check:
$('myLink').addEvent('click',function(e) {
if(e) e.stop();
//do stuff
});
Voila. The safe way to stop events!
![Facebook Open Graph META Tags]()
It's no secret that Facebook has become a major traffic driver for all types of websites. Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly. And of course there are Facebook "Like" and "Recommend" widgets on every website. One...
![Detect DOM Node Insertions with JavaScript and CSS Animations]()
I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API.
![iPad Detection Using JavaScript or PHP]()
The hottest device out there right now seems to be the iPad. iPad this, iPad that, iPod your mom. I'm underwhelmed with the device but that doesn't mean I shouldn't try to account for such devices on the websites I create. In Apple's...
![Create a Brilliant Sprited, CSS-Powered Firefox Animation]()
Mozilla recently formally announced Firefox OS and its partners at Mobile World Congress and I couldn't be more excited. Firefox OS is going to change the lives of people in developing countries, hopefully making a name for itself in the US as well. The...
Nice one, thanks.
I was using e.preventDefault()
:)
Doesn’t “return false;” handle this just as well (assuming you’re only adding one function to the event)?
Bob: http://blog.calyptus.eu/seb/2009/07/why-you-shouldnt-return-false-in-mootools-event-handlers/
I’m using
new Event(e).stop();
nice trick,
I was facing this problem a lot of them,
Thanks.
@David Walsh: Thanks, that’s good info. I hadn’t run into that as an issue, but will keep this in mind going forward.
I do this all the time, it’s always nice to see other developers handling it the same way.
I prefer the syntax, considering it saves a whole byte:
&& e.stop();
Great solution. I spent a day trying to find a solution on similar problem. David you can’t imagine how much I love your blog.
Granted you may have some special reason for fake-firing an event in which case your solution is fine. But more generally a better practise is to have your event handler call a function. So if you need to make that function happen, just call your function from code rather than faking the event and tripping the event handler in a fake way. Maybe you can give an example of when it’s not possible to use my suggestion?
Thanks,
Paul S
@Paul Schwarz: Your solution require to remember what function is connected to event. Sometimes it’s anonymous functions and you can’t call it directly. Little example:
@Paul Schwarz: Another example is, you can have plugins on page that do something with your elements, and don’t have access to this functions. It can be more that one function connected to element, and from other sources than yours. So basically calling: myelement.fireEvent you will call all functions attached to the event, and you don’t have to remember or search for it.
e.stop();
works for me as well. What I am looking for is a way to kill all the events (never to fire again). Would help a lot to deal with app “crashes” when timers and mouse, etc. events are involved.