peter
peter

Reputation: 42192

vbscript: getref with parameter

has anyone experience with passing a parameter to a function called with getref ? Following code is just an example, doens't work, how to pass the parameter to the mySub sub?

<button id="myBtn">Click me</button>

<script type="text/vbscript">
  document.getElementById("myBtn").onclick=GetRef("mySub") 

  Sub mySub(parameter)
   alert(parameter)
  End Sub
</script>

Upvotes: 4

Views: 5798

Answers (4)

iRon
iRon

Reputation: 23663

One step further...

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

Thus:

.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 can't use the Me reference.

Upvotes: 1

peter
peter

Reputation: 42192

The solution of Anthony is clever and i accespt his answer. However since my real problem was situated in a vbs rather than a clientscript which i only chose as example for simplicity it wasn't the solution for me.

Here is how i did it for reference. I used execute in stead of getref. eg

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

EDIT: as a result of Ekkehard's comment i tried it with his technique and it works, so instead of my first workaround i'm gonna use this solution, here what i should have asked from the first time but i was afraid it was too complicated.. I give the answer to him instead.

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

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

First look at this article about event handling (anybody knows a better reference?) to get the context for:

The code provided in the onclick attribute will be called when the user clicks on the text enclosed in the span. This mechanism is great for small snippets of code, but it becomes cumbersome when you have a lot of script. This event mechanism works with both VBScript and JScript.

What happens behind the scenes is that Internet Explorer calls into the script engine with the script code and tells the engine to create an anonymous function (a function with no name). Those of you who know VBScript are probably wondering how it does this, since VBScript doesn't support anonymous functions. VBScript actually creates a subroutine called "anonymous" containing the script and returns a pointer to the function that is then hooked up 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, i.e. 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

  1. that/how you can specify a Sub with no params to handle clicks literally or via GetRef (A resp. B)
  2. that you can use one parameterized Sub to handle clicks on many buttons, because the engine puts the literal code into an anonymous Sub (with no params) (C/D)
  3. that you can't use GetRef("SubWithLotsOfParms") to set the onClick attribute - it needs s Sub with no params
  4. that you can let a named Sub with no params (e.g. Magic) do the work of the engine's anonymous one; this Sub then can be used with GetRef

WRT Salman A's answer:

If you really need an error message like:

---------------------------
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 have 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 test .hta.

WRT Peter's proposal - it pays to keep it 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

That you use the name of the variable set with GetRef() exactly as you use the literal Sub/Function name could have been established yesterday.

Upvotes: 2

AnthonyWJones
AnthonyWJones

Reputation: 189457

Here is how I would achieve this:

        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 on which the onclick is being assigned to carry any parameters needed as additional attributes.

Upvotes: 3

Related Questions