English 中文(简体)
vbscrip: 带参数的gerref
原标题: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>
最佳回答

首次查看 < a href="" "http://msdn.microsoft.com/ en-us/library/ms974564.aspx" rel="nofollow" 有关事件处理 (有人知道更好的参考吗? ) 的文章以获取上下文 :

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 和 JScript.

What happens behind the scenes is that Internet Explorer calls into the script engine with the script code 和 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 和 returns a pointer to the function that is then hooked up to the event.

然后实验这个.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 SetClickH和lers()
     Set bttB.onClick = GetRef("NoParmsBttB")
     Set bttE.onClick = GetRef("Magic")
     Set bttF.onClick = GetRef("Magic")
   End Sub
     trivial h和ler, literally set
   Sub NoParmsBttA()
     Log "NoParmsBttA() called."
   End Sub
     trivial h和ler, set via GetRef
   Sub NoParmsBttB()
     Log "NoParmsBttB() called."
   End Sub
     one h和ler for many buttons, literally set
   Sub h和leClickCD(oBtt)
     Log "h和leClickCD() called; you clicked " & oBtt.id
   End Sub
     one h和ler for many buttons, set via Magic() & GetRef
   Sub h和leClickEF(oBtt, dtWhen)
     Log "h和leClickEF() called; you clicked " & oBtt.id & " at " & CStr(dtWhen)
   End Sub
     stuffed via GetRef into onClick
   Sub Magic()
     h和leClickEF Me, Now
   End Sub
   Sub Log(s)
     MsgBox s, 0, Now
   End Sub
  </SCRIPT>
 </head>
  <body onLoad="SetClickH和lers">
   <!-- literal onClick h和ler in html code -->
   <button id="bttA" onClick="NoParmsBttA">A</button>
   <!-- no literal onClick h和ler, will be set by SetClickH和lers via GetRef() -->
   <button id="bttB">B</button>
   <!-- literal onClick h和lers with parameter (Me, i.e. the Button) -->
   <button id="bttC" onClick="h和leClickCD Me">C</button>
   <button id="bttD" onClick="h和leClickCD Me">D</button>
   <!-- Two params h和ler via SetClickH和lers & Magic -->
   <button id="bttE">E</button>
   <button id="bttF">F</button>
 </body>
</html>

查看

  1. that/how you can specify a Sub with no params to h和le clicks literally or via GetRef (A resp. B)
  2. that you can use one parameterized Sub to h和le 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回答:

如果您真的需要错误消息, 例如 :

---------------------------
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   
---------------------------

然后,你只需要加上:

   Sub mySub(parameter)
     alert(parameter.toString())
   End Sub

   <!-- literal onClick h和ler in html code -->
   <button id="bttG" onClick="mySub">G</button>

测试。 。 hta 。

WRT Peter 的提议,

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

输出 :

cscript 01.vbs
1 2 3 4
1 2 3 4

您可以使用 GetRef () 的变量集的名称, 正如您使用 literal Sub/Function 名称 一样, 昨天可能已经建立 。

问题回答

以下是我如何实现这一点:

        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   

使用指定点击的元素,以携带作为额外属性所需的任何参数。

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.

我用执行来代替freef. 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

再往前一点...

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

因此:

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

或者:

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

或甚至:

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

唯一的限制是您不能使用 < 坚固 > Me < /坚固 > 引用 。





相关问题
How to import excel file in web page useing Classic ASP?

We are trying to open an existing excel file (2003) from server location in a web page and save it again in the same location using following syntax. Set ExcelReportApp = CreateObject("Excel....

What s the best method to do paging in my ASP page

What s the best method to do paging in my ASP page when displaying a list of items? I knew that there is no equivalent to MySQL s LIMIT clause present in SQL Server, either 2000 or 2005. How can I ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

Response.Redirect HTTP status code

Why is it that ASP/ASP.NET Response.Redirect uses a HTTP-302 status code ("Moved Temporarily") even though in most cases a HTTP-301 status code ("Moved Permanently") would be more appropriate?

form inside form serialize problem

I am trying to submit a form inside another form because I will need first form s outcome in the second form. I tried using form serialize as advised in some other threads. Problem here is, I dont ...

热门标签