Page 142 - DCAP202_Fundamentals of Web Programming
P. 142

Unit 11: Handling Events Using JavaScript




          11.1 Working on Event                                                                 Notes

          Events are normally used in combination with functions, and the function will not be executed
          before the event occurs!

          11.1.1 onLoad and onUnload

          The onLoad and onUnload events are triggered when the user enters or leaves the page.
          The onLoad event is often used to check the visitor’s browser type and browser version, and
          load the proper version of the web page based on the information.
          Both the onLoad and onUnload events are also often used to deal with cookies that should be set
          when a user enters or leaves a page.


                 Example: You could have a popup asking for the user’s name upon his first arrival to
          your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you
          could have another popup saying something like: “Welcome upendra!”.

          11.1.2 onFocus, onBlur and onChange

          The onFocus, onBlur and onChange events are often used in combination with validation of
          form fields.
          Below is an example of how to use the onChange event. The checkEmail() function will be called
          whenever the user changes the content of the field:
          <input  type=”text”  size=”30"  id=”email”  onchange=”checkEmail()”>

          11.1.3 onSubmit

          The onSubmit event is used to validate ALL form fields before submitting it.
          Below is an example of how to use the onSubmit event. The checkForm() function will be called
          when the user clicks the submit button in the form. If  the field values are not accepted, the
          submit should be cancelled. The function checkForm() returns either true or false. If it returns
          true the form will be submitted, otherwise the submit will be cancelled:
          <form  method=”post”  action=”xxx.htm”  onsubmit=”return  checkForm()”>

          11.1.4 onMouseOver and onMouseOut

          onMouseOver and onMouseOut are often used to create “animated” buttons.
          Below is an example of an onMouseOver event. An alert box appears when an onMouseOver
          event is detected:
          <a  href=”a.html”  onmouseover=”alert(‘An  onMouseOver  event’);return  false
          </a>
          Self Assessment

          Fill in the blanks:
          1.   .............................. are normally used in combination with functions, and the function will
               not be executed before the event occurs!
          2.   The .............................. and onUnload events are triggered when the user enters or leaves
               the page.




                                           LOVELY PROFESSIONAL UNIVERSITY                                   135
   137   138   139   140   141   142   143   144   145   146   147