VBScript - Events


Advertisements

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. Please go through this small tutorial for a better understanding HTML Event Reference. Here, we will see few examples to understand a relation between Event and VBScript.

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 Howcodex.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.

Event Value Description
onchange script Script runs when the element changes
onsubmit script Script runs when the form is submitted
onreset script Script runs when the form is reset
onblur script Script runs when the element loses focus
onfocus script Script runs when the element gets focus
onkeydown script Script runs when key is pressed
onkeypress script Script runs when key is pressed and released
onkeyup script Script runs when key is released
onclick script Script runs when a mouse click
ondblclick script Script runs when a mouse double-click
onmousedown script Script runs when mouse button is pressed
onmousemove script Script runs when mouse pointer moves
onmouseout script Script runs when mouse pointer moves out of an element
onmouseover script Script runs when mouse pointer moves over an element
onmouseup script Script runs when mouse button is released
Advertisements