My First Post Of Shame - ReturnFalse()?
-
OK, Let me preface this by saying, I'm NOT a professional programmer. So I may be way off base and just lacking the knowledge as to why this may NOT be a piece of Shame. If there is a reason to do this, I'd love to know it.. :) Anyhow... Found this in some JavaScript in a vendor's product we pay plenty of money into monthly:
function ReturnFalse()
{
return false;
}The only place I can find it used it in a couple statements like this:
if (button.onclick != ReturnFalse) {...
and
button.onclick = ReturnFalse;
Am I nuts or are they? :)
-
OK, Let me preface this by saying, I'm NOT a professional programmer. So I may be way off base and just lacking the knowledge as to why this may NOT be a piece of Shame. If there is a reason to do this, I'd love to know it.. :) Anyhow... Found this in some JavaScript in a vendor's product we pay plenty of money into monthly:
function ReturnFalse()
{
return false;
}The only place I can find it used it in a couple statements like this:
if (button.onclick != ReturnFalse) {...
and
button.onclick = ReturnFalse;
Am I nuts or are they? :)
The shame is not what you think it is. They are assigning a function to a click event. This function returns false. In JavaScript, this prevents the event from doing what it does by default. So, if you have:
<a href="#">Click Me</a>
And you assign the ReturnFalse function to it, clicking it will not append "#" to the URL. Without assigning that click handler, it would append that to the URL. The shame is that they don't attach the event and that they compare the event to the function. They should avoid comparing completely, and they should use something like jQuery's $.bind function to add the function as a handler. Assigning a function to an event will replace all existing handlers, which should be avoided. However, if they wan't to remove all existing handlers, that may be the ideal approach.
-
The shame is not what you think it is. They are assigning a function to a click event. This function returns false. In JavaScript, this prevents the event from doing what it does by default. So, if you have:
<a href="#">Click Me</a>
And you assign the ReturnFalse function to it, clicking it will not append "#" to the URL. Without assigning that click handler, it would append that to the URL. The shame is that they don't attach the event and that they compare the event to the function. They should avoid comparing completely, and they should use something like jQuery's $.bind function to add the function as a handler. Assigning a function to an event will replace all existing handlers, which should be avoided. However, if they wan't to remove all existing handlers, that may be the ideal approach.
Ahhh OK.... So because they are assigning it to an OnClick event, then it needs to be a function instead of just a simple Boolean, and they're trying to have the click do 'nothing', but do this to avoid having the #, instead of just assigning the OnClick to null, or removing the OnClick attribute completely? Weird... But good to know for sure... Better I learn this trick, than lose faith in the darned supplier I guess. ;)
-
Ahhh OK.... So because they are assigning it to an OnClick event, then it needs to be a function instead of just a simple Boolean, and they're trying to have the click do 'nothing', but do this to avoid having the #, instead of just assigning the OnClick to null, or removing the OnClick attribute completely? Weird... But good to know for sure... Better I learn this trick, than lose faith in the darned supplier I guess. ;)
That is one confusing sentence, but I think you've got the basic idea.
-
That is one confusing sentence, but I think you've got the basic idea.
-
The shame is not what you think it is. They are assigning a function to a click event. This function returns false. In JavaScript, this prevents the event from doing what it does by default. So, if you have:
<a href="#">Click Me</a>
And you assign the ReturnFalse function to it, clicking it will not append "#" to the URL. Without assigning that click handler, it would append that to the URL. The shame is that they don't attach the event and that they compare the event to the function. They should avoid comparing completely, and they should use something like jQuery's $.bind function to add the function as a handler. Assigning a function to an event will replace all existing handlers, which should be avoided. However, if they wan't to remove all existing handlers, that may be the ideal approach.
-
Depends on if you want to dynamically toggle the functionality. And there are benefits to keeping your markup clean from inline JavaScript. Also, "önclick" will probably not work. :)
-
Depends on if you want to dynamically toggle the functionality. And there are benefits to keeping your markup clean from inline JavaScript. Also, "önclick" will probably not work. :)
it wont work, i wonder how i wrote that character?
-
it wont work, i wonder how i wrote that character?
If you are using a "US - International" keyboard layout, you can type that character by typing a quote mark followed by the letter "o".
-
Depends on if you want to dynamically toggle the functionality. And there are benefits to keeping your markup clean from inline JavaScript. Also, "önclick" will probably not work. :)
-
because i am Turkish I get it :)
-
The shame is not what you think it is. They are assigning a function to a click event. This function returns false. In JavaScript, this prevents the event from doing what it does by default. So, if you have:
<a href="#">Click Me</a>
And you assign the ReturnFalse function to it, clicking it will not append "#" to the URL. Without assigning that click handler, it would append that to the URL. The shame is that they don't attach the event and that they compare the event to the function. They should avoid comparing completely, and they should use something like jQuery's $.bind function to add the function as a handler. Assigning a function to an event will replace all existing handlers, which should be avoided. However, if they wan't to remove all existing handlers, that may be the ideal approach.
I think it would be less misleading if the ReturnFalse function was instead named AbortAction or something closer to the intention. I think it is better practise to name a function or method for it's intended purpose or meaning than for what it literally does. The intention/meaning and what the method actually does usually are the same, but there can be subtle differences and occasionally vast differences such as in this case. Oh and a comment for the function would not go amiss either:
//This function is used to suppress events such as onclick
function AbortAction()
{
return false;
}