Part of the error about the OnLoad event in JS.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.