English 中文(简体)
Javascript - Events
  • 时间:2024-11-03

JavaScript - Events


Previous Page Next Page  

What is an Event ?

JavaScript s interaction with HTML is handled through events that occur when the user or the browser manipulates a page.

When the page loads, it is called an event. When the user cpcks a button, that cpck too is an event. Other examples include events pke pressing any key, closing a window, resizing a window, etc.

Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be vapdated, and virtually any other type of response imaginable.

Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of events which can trigger JavaScript Code.

Please go through this small tutorial for a better understanding HTML Event Reference. Here we will see a few examples to understand a relation between Event and JavaScript −

oncpck Event Type

This is the most frequently used event type which occurs when a user cpcks the left button of his mouse. You can put your vapdation, warning etc., against this event type.

Example

Try the following example.

<html>
   <head>   
      <script type = "text/javascript">
         <!--
            function sayHello() {
               alert("Hello World")
            }
         //-->
      </script>      
   </head>
   
   <body>
      <p>Cpck the following button and see result</p>      
      <form>
         <input type = "button" oncpck = "sayHello()" value = "Say Hello" />
      </form>      
   </body>
</html>

Output