It seems like I type "function(){return false;}" some what too frequently too prevent actions from happening... is there a short cut?
-
You 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 tofalse
, and usefunction() {return f;}
Ates Goral : function f() { return false; }Chris Noe : Seems like a false economy :-7zildjohn01 : @Ates -- got me there :) -
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.
Unkwntech : I'm thinking call it something shorter like ff() (bad name I know) and so that it's even shorter but this seems like the right idea.Luca Matteis : What, this will not work, -1, please explain.Luca Matteis : Oh, "always_false" would be the actual listener?Luca Matteis : then I would expand that, since he's using jquery to: function always_false(e){e.preventDefault();} -
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.Luca Matteis : return false; doesn't "cancel event"s.Crescent Fresh : @Luca: try it some time. -
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;
0 comments:
Post a Comment