Discussion:
From VB6 code - run Javascript function in WebBrowser control
(too old to reply)
kjm2
2007-11-02 08:11:58 UTC
Permalink
Hi,

Can this be done? Can VB6 code execute a javascript function in a loaded page
of a webbrowser control?

I have a VB6 project with a WebBrowser control on it with this page loaded.

--------------------------------------
<html>
<body>
<script>
function showalert(){
alert("Hello World");
}
</script>
<p>my web page</p>
<input type="button" onclick="showalert()" value="Click for Hello World">
</body>
</html>
--------------------------------------

In a cmdButton_Click() event I'd like to call the javascript function
showalert().


Thanks
Ken
Igor Tandetnik
2007-11-02 12:12:02 UTC
Permalink
Post by kjm2
Can this be done? Can VB6 code execute a javascript function in a
loaded page of a webbrowser control?
window.execScript
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
kjm2
2007-11-02 18:52:33 UTC
Permalink
Thanks for the reply but after doing a bunch of searching on the web for
window.execScript I don't think that is what I need. That will execute the
script I pass it but what Iwant to do is to execute an existing script/function
that is loaded in a BrowserControl.

Something like: WebBrowser.Document.??? where I can run the existing fuction
in a javascript function loaded in the current browser control's loaded page.

Thanks
Ken
Post by Igor Tandetnik
Post by kjm2
Can this be done? Can VB6 code execute a javascript function in a
loaded page of a webbrowser control?
window.execScript
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not necessarily a
good idea. It is hard to be sure where they are going to land, and it could be
dangerous sitting under them as they fly overhead. -- RFC 1925
Igor Tandetnik
2007-11-02 20:07:56 UTC
Permalink
Post by kjm2
Thanks for the reply but after doing a bunch of searching on the web
for window.execScript I don't think that is what I need. That will
execute the script I pass it but what Iwant to do is to execute an
existing script/function that is loaded in a BrowserControl.
You can pass a script fragment that consists of a function call:

window.execScript("myFunction();", "JavaScript")
Post by kjm2
Something like: WebBrowser.Document.??? where I can run the
existing fuction in a javascript function loaded in the current
browser control's loaded page.
WebBrowser.Document.parentWindow.execScript(...)

You should also be able to call the function via late binding, but I'm
not exactly sure how to express this in VB. Try this:

Dim w as Object
Set w = WebBrowser.Document.parentWindow
w.myFunction ' myFunction is the name of the function in the script
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Loading...