Vbscript: getref with parameter

Does anyone have any experience passing a parameter to a function called with getref? The following code is just an example, doens't work, how do I pass the mySub sub parameter?

<button id="myBtn">Click me</button> <script type="text/vbscript"> document.getElementById("myBtn").onclick=GetRef("mySub") Sub mySub(parameter) alert(parameter) End Sub </script> 
+3
source share
4 answers

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> <!-- !! http://stackoverflow.com/questions/10741292/vbscript-getref-with-parameter --> <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"> <!-- literal onClick handler in html code --> <button id="bttA" onClick="NoParmsBttA">A</button> <!-- no literal onClick handler, will be set by SetClickHandlers via GetRef() --> <button id="bttB">B</button> <!-- literal onClick handlers with parameter (Me, ie the Button) --> <button id="bttC" onClick="handleClickCD Me">C</button> <button id="bttD" onClick="handleClickCD Me">D</button> <!-- Two params handler via SetClickHandlers & Magic --> <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

  <!-- literal onClick handler in html code --> <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.

+2
source

Here is how I would do it:

  Dim elem: Set elem = document.getElementById("myBtn") elem.setAttribute "parameter", "somevalue" Set elem.onclick = GetRef("elem_onclick") Function elem_onclick() MsgBox Me.getAttribute("parameter") End Function 

Uses the element to which onclick is assigned to carry any parameters needed as additional attributes.

+2
source

Anthony's decision is smart, and I join his answer. However, since my real problem was located in vbs, and not in the client script, which I chose only as an example for simplicity, this was not a solution for me.

Here is how I did it for reference. I used execution instead of getref. eg,

 execute ("call " & routine & "(parameter)") 

EDIT: as a result of Ekkehard's comment, I tried it using my technique and it works, so instead of my first workaround, I am going to use this solution, this is what I had to ask the first time, but I was afraid it was too difficult .. Instead of this I give him an answer.

 sub one(para) WScript.Echo para & " from one" end sub sub two(para) WScript.Echo para & " from two" end sub sub main(subname, para) Dim f : Set f = GetRef(subname) f para end sub main "one", "test" '=>test from one 
+1
source

One more step ...

 Dim iGetSub 'Static' Function GetSub(sSub) 'GetRef wrapper that supports object methods and arguments iGetSub = iGetSub + 1 ExecuteGlobal "Sub GetSub" & iGetSub & ": " & sSub & ": End Sub" Set GetSub = GetRef("GetSub" & iGetSub) End Function 

In this way:

 .onClick = GetSub("MySub(""Hello World"")") 

Or:

 .onClick = GetSub("MsgBox(""Hello World"")") 

Or even:

 .onClick = GetSub("MyObject.MyMethod(""Hello World"")") 

The only limitation is that you cannot use the Me link.

+1
source

Source: https://habr.com/ru/post/988842/


All Articles