How NOT to Code - Challenge 13
Feb 11, 2010
Today's challenge is in Javascript. See how many things you can pick out that's wrong with this code ;)
<script language="JavaScript">
<!-- Begin
function checkFields() {
missinginfo = "";
if (document.form.name.value == "") {
missinginfo += "\n - Name";
}
if (missinginfo != "") {
missinginfo = "Correctly fill in your:\n" +
missinginfo + "\n and submit again!";
alert(missinginfo);
return false;
}else{
return true;
}
}
//-->
</script>
Garrett Johnson wrote on 02/11/104:15 PM
Oh man...1) Bad comment wrapped around.
2) Global variable no no no no no.
3) document.form will error, its an array so technically should be document.forms[0], but really you should just use an Id to speed things up...
4) Don't use the "==" or "!=" equality operators, use the strict ones: "===" and "!==".