|
||||||
How to Program Button Events with VBScriptUsing VBScript to Handle Button and Mouse Events
Internet Explorer is the one web browser that uses VBScript and because of that it is very powerful - especially when it comes to handling events such as button clicks
It's not often that any web browser has a definite advantage over any other one, however, Internet Explorer does have one major advantage - VBScript; and it's not just the versatility of VBScript that makes it attractive - it's VBScript's ease of use. Take, for example, buttons on a web page. Each button on a web page has a number of built-in events that can be triggered by the web page user and these events are:
And the beauty of these events is that each event has a subroutine associated with it. The Onclick SubroutineThe obvious event for any button is the onclick event - but before the onclick event can be used the button needs to be defined using HTML: <h1>VBSCript Button Events</h1>
<input type=button value="Click Me" id=button_1>
<div id=output_text></div>
Next the onclick subroutine is required - and the name of the subroutine always follows the same convention:
So, for example, the name for the subroutine, in this case, will be button_1_onclick: <script language=vbscript>
sub button_1_onclick
output_text.innerHTML = "You clicked me"
end sub
</script>
If the web page is loaded into Internet Explorer and the button is clicked then the div area will be written to. The Onmouseover and Onmouseout SubroutinesAs well as having events associated with it, each button is also self-referencing, for example: sub button_1_onmouseover
me.value = "Go on - Click me"
end sub
In this example the text of the button is updated when the mouse pointer is placed over it and, of course, the text can be changed back again when the mouse pointer is moved away from the button: sub button_1_onmouseout
me.value = "Click me"
end sub
A suitable use for this may be to supply the user with instructions before they click the button. The Ondblclick SubroutineAs well being able to click on the button, the user can also double-click on the the button: sub button_1_ondblclick
output_text.innerHTML = "Calm down - once is enough"
end sub
And in addition to clicking and double-clicking the button two other events are associated with the button click - onmousedown and onmouseup. The Onmousedown and Onmouseup SubroutinesThe onmousedown subroutine is triggered when the mouse button is depressed over the button (and fires before the onclick subroutine): sub button_1_onmousedown
window.status = "Clicking " & me.id
end sub
And the onmouseup subroutine is triggered when the mouse button is released (and fires after the onclick subroutine): sub button_1_onmouseup
window.status = me.id & " clicked"
end sub
In both these examples the window.status is written to - that's the bar at the bottom of the Internet Explorer display. The final subroutine fires whenever the user moves the mouse pointer over the button - but this should be used with care. The Onmousemove SubroutineThe usage of the onmousemove subroutine should be limited as much as possible - simply because the subroutine is triggered whenever the user moves the mouse, for example: <div id=mouse_moves></div>
<script language=vbscript>
dim mousemove_count : mousemove_count = 0
sub button_1_onmousemove
mousemove_count = mousemove_count + 1
mouse_moves.innerHTML = mousemove_count
end sub
</script>
The example shows that this event can be triggers hundreds of times per minute. SummaryEach HTML button in Internet Explorer has a number of events associated with it:
And each event has its own subroutine which is named after the button and the event, all of which the web page programmer can implement very easily.
The copyright of the article How to Program Button Events with VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Program Button Events with VBScript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||