2

I have a VBS application with a webbrowser control. In this control I open an HTML file which contains JavaScript. This JavaScript just sets a variable to value, like this:

var clicked = 3;

Do I have any possibilty to use this variable in my VBS application? Can I return the value?

Thank you!


in my Webbrowswer control I use the event DocumentComplete? And in this event I say:

Dim oScript As Object
set myVariables = oScript.clicked

? If I have more than one global variable, how can I chose the right one? And If I set this variable in javascript in an OnClick event the event "DocumentComplete" is not the right one, or? And the line Dim oScript As Object is not accepted in Viszal Basic Script?

Thank you for your help!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
swcomputer
  • 21
  • 2
  • Can you elaborate on what you mean by VBS application? I am guessing it could be (1) HTA Application, (2) Web App (i.e. web page), (3) ASP, (4) CScript/WScript or (5) ScriptHost application. – Stephen Quan Jan 21 '12 at 13:30
  • It is a Visual Basic Script 6.0 Application. And there I've got a form with a Webbrowser Control. So I think it is WScript. – swcomputer Jan 21 '12 at 19:44
  • Okay, I understand. You have a Visual Basic application (not VBScript; your question is misleading and mistagged). Your wish to share content between your javascript and your Visual Basic application. – Stephen Quan Jan 22 '12 at 00:25

2 Answers2

0

I know this answer may not be applicable to Visual Basic 6.0, but, in VB .NET and C# you do something like this: WebBrowser1.Document.InvokeScript("eval", "clicked"). Perhaps you can find something similar in Visual Basic 6.0.

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
0

VB6 is good at late binding, so you do not need to deal with automation stuff like IDispatch and DISPATCH_PROPERTYGET. Just Dim oScript As Object and set it to webbrowser.document.script in the DocumentComplete event. DocumentComplete is also a good place to replace element handlers, see handle html element events, in case you want to sink element events, then you can call oScript.clicked to get the global variable's value. That is, unless you turn on the IE9 mode in your webbrowser control, then you need to deal with IDispatchEx and COM madness.

Read Javascript variable from Web Browser control is the C# version of this question.

Community
  • 1
  • 1
Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • My question was just an example. In my Javascript I set this variable "clicked" in an OnClick-Event. So the Webbrowser Event "DocumentFinished" is not the right one, or? And in Visual Basic Script I think there is no solution to say "Dim oScript As Object" I think you have to create an object. Can you please explain a little bit more? In my Webbrowser Event I just say "oScript.clicked"?? And this .clicked is my variable in Javascript? – swcomputer Jan 24 '12 at 15:28
  • If you set the variable later, then you need to check its value later, document complete is before the user click. And I was assuming you having a VB app, how did you write an ActiveX host in VBS? – Sheng Jiang 蒋晟 Feb 22 '12 at 16:38