Small experience in JavaScript non-trivial syntax
June 26th, 2007 by Denis GolovtsovFew days ago I read interesting article about non-trivial syntax of JavaScript.
And today I had the chance to met using of this kind magic in real practice. Let me show this small example.
I have a simple form with one field which always have to be filled for success submitting, to reduce a cycles of interaction between server and client browser I’d like to use JavaScript that will check form before submitting and prevent cause when user forgot fill value. Below I show the piece of code that we can solve this problem:
// <![CDATA[
function check(f)
{
if (f.fld.value == ”)
{
alert(‘Should be filled!’);
f.fld.focus();
return false;
}
return true;
}
// ]]>
</script>
<form method=“get” onsubmit=“return check(this)”>
<input name=“fld” maxlength=“255″ type=“text” />
<input value=“Submit” type=“submit” />
</form>
I think it’s cleae and doesn’t require any explanations. Now suggest hypothetical situation when we don’t like write special function for checking only one field and would like to put it directly in onchange attribute, how will code look in this case?
This code looks terrible and mixed, but I wouldn’t like to discuss it here, because my purpose here in other. I’d like to show way to use inline handlers function in general. That’s all.