Ask:
Hello, Scripting Guy! In your HTA example, you showed us how to click a button to make a subroutine run. So how do you add two or more child routines to a button's onClick argument?
--FM
For:
Hello, FM. You know, there's an interesting branch of cognitive psychology called Problem discovery, and the theory is that your ability to answer questions is usually the first thing you ask. For example, you – and several others – want to know how to specify multiple subroutines in a button's onClick argument. We've seen people try to make various changes to the code, and here's an example:
<input type= "button" value= "Run button" onclick= "script_1; script_2; Script_3 ">
As you can see, this does not solve the problem.
So let's apply the problem-finding skills to the test and see if we can describe the problem in other ways. (Usually, the Scripting Guys don't need to find the problem; The problem will try to find us.) Below is the crux of the problem: are we really going to add more than one subroutine to the OnClick argument or just want to run multiple subroutines at any point when you click a button?
If it is the latter, we have the following answer:
Copy Code code as follows:
<title>multiple subroutines</title>
Id= "Objhtahelpomatic"
Applicationname= "Multiplesubroutines"
Scroll= "Yes"
Singleinstance= "Yes"
Windowstate= "Maximize"
>
<script language= "VBScript" >
Sub runscripts
Script_1
Script_2
Script_3
End Sub
Sub script_1
Msgbox "This is subroutine 1."
End Sub
Sub script_2
Msgbox "This is subroutine 2."
End Sub
Sub Script_3
Msgbox "This is subroutine 3."
End Sub
</SCRIPT>
<body>
<input type= "button" value= "Run button" onclick= "Runscripts" >
</body>
Note: The previous code was designed to run from an HTA (HTML application). If you want to test the code, simply copy the script, paste it into Notepad or another text editor, and use the. HTA file name extension saved.
If you look at the code roughly, you may notice the HTML markup for the button:
<input type= "button" value= "Run button" onclick= "Runscripts" >
As you can see, we only specify a single subroutine (runscripts) in the OnClick parameter. Oh, but look at a sudden routine runscripts code:
Sub runscripts
Script_1
Script_2
Script_3
End Sub
The answer is right here. All we do in this subroutine is invoke the other three subroutines: Script_1, Script_2, and Script_3. That's how we run multiple subroutines from a button's Click event: We don't put all those subroutines into the onclick argument, but instead put it in a single subroutine called by the onclick.