Page 155 - CITS - Computer Software Application -TT
P. 155

COMPUTER SOFTWARE APPLICATION - CITS



           form.addEventListener(“submit”, function (event) {

               // stop form submission
               event.preventDefault();
               // validate the form
               let nameValid = hasValue(form.elements[“name”], NAME_REQUIRED);
               let emailValid = validateEmail(form.elements[“email”], EMAIL_REQUIRED, EMAIL_INVALID);

               // if valid, submit the form.
               if (nameValid && emailValid) {
                       alert(“Demo only. No form was posted.”);
               }
           });

           Code language: JavaScript (javascript)
           In the submit event handler:
           1  Stop the form submission by calling the event.preventDefault() method.
           2  Validate the name and email fields using the hasValue() and validateEmail() functions.
           3  If both name and email are valid, show an alert. In a real-world application, you need to call the form.submit()
              method to submit the form.
           Summary
           •  Use the <form> element to create an HTML form.

           •  Use DOM methods such as getElementById() and querySelector() to select a <form> element. The document.
              forms[index] also returns the form element by a numerical index.
           •  Use form.elements to access form elements.
           •  The submit event fires when users click the submit button on the form.

             Concept of Cookies


           Cookies let you store user information in web pages.
           What are Cookies?
           Cookies are data, stored in small text files, on your computer.
           When a web server has sent a web page to a browser, the connection is shut down, and the server forgets
           everything about the user.
           Cookies were invented to solve the problem “how to remember information about the user”:
           •  When a user visits a web page, his/her name can be stored in a cookie.

           •  Next time the user visits the page, the cookie “remembers” his/her name.
           Cookies are saved in name-value pairs like:
           username = John Doe
           When a browser requests a web page from a server, cookies belonging to the page are added to the request. This
           way the server gets the necessary data to “remember” information about users.
           None of the examples below will work if your browser has local cookies support turned off.
           Create a Cookie with JavaScript
           JavaScript can create, read, and delete cookies with the document.cookie property.
           With JavaScript, a cookie can be created like this:



                                                           142

                             CITS : IT&ITES - Computer  Software Application - Lesson 37 - 46
   150   151   152   153   154   155   156   157   158   159   160