First look in this article on event handling (does anyone know a better link?) To get context for:
The code specified in the onclick attribute will be called when the user clicks on the text enclosed in the gap. This mechanism is great for small code snippets, but it gets cumbersome when you have a lot of script. This event engine works with both VBScript and JScript.
What happens behind the scenes is that Internet Explorer calls the script engine with script code and tells the engine to create an anonymous function (a function without a name). Those of you who know VBScript are probably wondering how this happens, since VBScript does not support anonymous functions. VBScript actually creates an anonymous routine containing the script and returns a pointer to a function that then connects to the event.
Then experiment with this .hta:
<html> <head> <title>GetRef HTA</title> <HTA:APPLICATION APPLICATIONNAME="GetRef HTA" > <SCRIPT Language="VBScript"> Sub SetClickHandlers() Set bttB.onClick = GetRef("NoParmsBttB") Set bttE.onClick = GetRef("Magic") Set bttF.onClick = GetRef("Magic") End Sub ' trivial handler, literally set Sub NoParmsBttA() Log "NoParmsBttA() called." End Sub ' trivial handler, set via GetRef Sub NoParmsBttB() Log "NoParmsBttB() called." End Sub ' one handler for many buttons, literally set Sub handleClickCD(oBtt) Log "handleClickCD() called; you clicked " & oBtt.id End Sub ' one handler for many buttons, set via Magic() & GetRef Sub handleClickEF(oBtt, dtWhen) Log "handleClickEF() called; you clicked " & oBtt.id & " at " & CStr(dtWhen) End Sub ' stuffed via GetRef into onClick Sub Magic() handleClickEF Me, Now End Sub Sub Log(s) MsgBox s, 0, Now End Sub </SCRIPT> </head> <body onLoad="SetClickHandlers"> <button id="bttA" onClick="NoParmsBttA">A</button> <button id="bttB">B</button> <button id="bttC" onClick="handleClickCD Me">C</button> <button id="bttD" onClick="handleClickCD Me">D</button> <button id="bttE">E</button> <button id="bttF">F</button> </body> </html>
to see
- what / how you can specify Sub without any parameters for processing clicks literally or via GetRef (A or B)
- that you can use one parameterized Sub to handle clicks on many buttons, because the engine puts a literal code in an anonymous Sub (without parameters) (C / D)
- that you cannot use GetRef ("SubWithLotsOfParms") to set the onClick attribute - it needs s Sub without parameters
- that you can allow the named Sub without parameters (for example, Magic) to execute an anonymous engine; this sub can then be used with getref
WRT Salman Answer:
If you really need an error message, for example:
--------------------------- Error --------------------------- A Runtime Error has occurred. Do you wish to Debug? Line: 54 Error: Wrong number of arguments or invalid property assignment: 'mySub' --------------------------- Yes No ---------------------------
then you just need to add:
Sub mySub(parameter) alert(parameter.toString()) End Sub
and
<button id="bttG" onClick="mySub">G</button>
to the .hta test.
WRT Peter proposal - pays to make things simple:
Option Explicit Sub WithLotsOfParms(a, b, c, d) WScript.Echo Join(Array(a, b, c, d)) End Sub Dim f : Set f = GetRef("WithLotsOfParms") WithLotsOfParms 1, 2, 3, 4 f 1, 2, 3, 4
output:
cscript 01.vbs 1 2 3 4 1 2 3 4
The fact that you are using the variable set name with GetRef () in the same way as you are using the literal name Sub / Function could be set yesterday.