webbrowser里的js与程序交互

jackxiang 2012-10-25 13:04 | |
比如在vb中有如下代码:
Private Function Test(S1,S2,S3)
Msgbox S1 & S2 & S3
End Function
现在在程序中嵌入webbrowser,要求在webbrowser网页js的onclick事件能够触发上面的函数。比如:
<a onclick="test('X','Y','Z')">文字</a>
我要详细代码,能够直接运行的那种,请高手赐教,谢谢!

------------------------------------------------------------------------------------------------------------------------------------------------
VBA的代码和vbs、js的代码虽然在语法有相似之处,但是结构完全不一样,VBA为编译代码,脚本为解释代码,两者不能实现直接交互,只能通过间接交互实现
这里通过VBScript修改浏览器标题,触发VB内部的TitleChange而实现交互


网页代码:
<a onclick=VBScript:document.title="VB:test('X','Y','Z')">文字</a>

窗体代码:
Option Compare Text

Private Function Test(S1, S2, S3)
MsgBox S1 & S2 & S3
End Function

Private Sub Form_Load()
WebBrowser1.Navigate "e:\1.htm"
End Sub

Private Sub WebBrowser1_TitleChange(ByVal Text As String)
If Left(Text, 3) = "VB:" Then
Dim s As String, fncName As String, l As Long, args
s = Mid(Text, 4)
s = Replace(s, "(", " ")
s = Replace(s, ")", " ")
l = InStr(s, " ")

fncName = Trim(Left(s, l - 1))
args = Split(Mid(s, l), ",")
For i = 0 To UBound(args)
  args(i) = Trim(args(i))
Next

Select Case fncName
  Case "test"
   Test args(0), args(1), args(2)
End Select
Else
Caption = Text
End If
End Sub

来源:http://zhidao.baidu.com/question/98643392.html&__bd_tkn__=2ebb4a25322b83604757a633bbba2fbec00bd2d08078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157154fb369e64a0fa40491fc7006f357ca033ca0e3f04937e782ac28ea13d310e772d335fce130a3b4fb0ddd99e08eabbc9da880acf3422f549

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/5692/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]