when using onload to get an element, it is recommended that you define the name of the element you want to get before the OnLoad event, and that only the fetch operation is performed in OnLoad, so that the acquired element will be used successfully later.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <Metahttp-equiv= "Content-type"content= "text/html"CharSet= "UTF-8"> <title>About Form</title></Head><Scripttype= "Text/javascript"> varusername; varMyForm; OnLoad=function() {username=document.getElementById ("username"); MyForm=document.getElementById ("MyForm"); }/*This write will appear error, MyForm variable written in onload, is a local variable, the execution of the form submission can not be called, should be the execution of the form submitted code also put in the onload inside. */Myform.onsubmit=function(){ if(Username.value=="name"){ return true; } return false; }</Script><Body><formAction=""Method=""ID= "MyForm" >User name:<inputtype= "text"ID= "username"> <inputtype= "Submit"value= "Submit"></form></Body></HTML>
The right way
<!DOCTYPE HTML><HTMLLang= "en"><Head> <Metahttp-equiv= "Content-type"content= "text/html"CharSet= "UTF-8"> <title>About Form</title></Head><Scripttype= "Text/javascript"> varusername; varMyForm; OnLoad=function() {username=document.getElementById ("username"); MyForm=document.getElementById ("MyForm"); Myform.onsubmit=function(){ if(Username.value=="name"){ return true; } return false; } } </Script><Body><formAction=""Method=""ID= "MyForm" >User name:<inputtype= "text"ID= "username"> <inputtype= "Submit"value= "Submit"></form></Body></HTML>
Part of the error about the OnLoad event in JS.