VBScript – Events

What is an Event ?

VBScript’s interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is an event. When the user clicks a button, that click too is an event. Other examples of events include pressing any key, closing window, resizing window, etc. Developers can use these events to execute VBScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable to occur.

Events are a part of the Document Object Model (DOM) and every HTML element has a certain set of events, which can trigger VBScript Code.

onclick Event Type

This is the most frequently used event type, which occurs when a user clicks mouse’s left button. You can put your validation, warning, etc., against this event type.

Example

<html>
   <head>
      <script language = "vbscript" type = "text/vbscript">
         Function sayHello() 
            msgbox "Hello World"
         End Function
      </script>
   </head>
   
   <body>
      <input type = "button" onclick = "sayHello()" value = "Say Hello"/>
   </body>
</html>

It will produce the following result, and when you click the Hello button, the onclick event will occur which will trigger sayHello() function.


onsubmit event type

Another most important event type is onsubmit. This event occurs when you try to submit a form. So you can put your form validation against this event type. The Form is submitted by clicking on Submit button, the message box appears.

The Form is submitted by clicking on Submit button, the message box appears.

Example

<html>
   <head> </head>
   <body>
      <script language = "VBScript">       
         Function fnSubmit()
            Msgbox("Hello Tutorialspoint.Com")
         End Function       
      </script>
      
      <form action = "/cgi-bin/test.cgi" method = "post" name = "form1" onSubmit = "fnSubmit()">
         <input name = "txt1" type = "text"><br>
         <input name = "btnButton1" type = "submit" value="Submit">
      </form>
   </body>
</html>

onmouseover and onmouseout

These two event types will help you to create nice effects with images or even with text as well. The onmouseover event occurs when you bring your mouse over any element and the onmouseout occurs when you take your mouse out from that element.

Example

<html>
   <head> </head>
   <body>
      <script language = "VBScript">       
         Function AlertMsg
            Msgbox("ALERT !")
         End Function
          
         Function onmourse_over()
            Msgbox("Onmouse Over")
         End Function

         Sub txt2_OnMouseOut()
            Msgbox("Onmouse Out !!!")
         End Sub
          
         Sub btnButton_OnMouseOut()
            Msgbox("onmouse out on Button !")
         End Sub
      </script>
      
      <form action = "page.cgi" method = "post" name = "form1">
         <input name = "txt1" type = "text" OnMouseOut = "AlertMsg()"><br>
         <input name = "txt2" type = "text" OnMouseOver = "onmourse_over()">
         <br><input name = "btnButton" type = "button" value = "Submit">
      </form>
   </body>
</html>

It will produce a result when you hover the mouse over the text box and also when you move the focus away from the text box and the button.

HTML 4 Standard Events

The standard HTML 4 events are listed here for your reference. Here, script indicates a VBScript function to be executed against that event.

EventValueDescription
onchangescriptScript runs when the element changes
onsubmitscriptScript runs when the form is submitted
onresetscriptScript runs when the form is reset
onblurscriptScript runs when the element loses focus
onfocusscriptScript runs when the element gets focus
onkeydownscriptScript runs when key is pressed
onkeypressscriptScript runs when key is pressed and released
onkeyupscriptScript runs when key is released
onclickscriptScript runs when a mouse click
ondblclickscriptScript runs when a mouse double-click
onmousedownscriptScript runs when mouse button is pressed
onmousemovescriptScript runs when mouse pointer moves
onmouseoutscriptScript runs when mouse pointer moves out of an element
onmouseoverscriptScript runs when mouse pointer moves over an element
onmouseupscriptScript runs when mouse button is released

Leave a Reply