It seems like I type "function(){return false;}" some what too frequently too prevent actions from happening... is there a short cut?
views:
233answers:
5You can cancel an event default action by using the preventDefault method:
$("form").bind("submit", function(event){
event.preventDefault();
});
Do you really think that's too much typing?
Why not have a global variable named f
set to false
, and use function() {return f;}
You could declare a named function like this:
function always_false() { return false; }
then use "always_false" wherever you would have created the anonymous function previously.
I often have a "no op" function defined at the top of my common js file:
function nop() { return false }
I use it whenever I need a "do nothing" or a "cancel event" handler (eg div.oncontextmenu = nop
). In IE this has the added benefit that memory does not leak when creating similar [anonymous] functions on the fly and assigning them to event handlers. At least that's what IE Drip tells me.
Just set the event handler to null. This is cleaner than assigning to a "return false" function IMO because it removes the event handler altogether.
myButton.onclick = null;