Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting return value from Javascript function via webbrowser control in VB

Posted on 2004-10-20
5
3,742 Views
Last Modified: 2008-01-09
I have spent a few days on this issue so far with no results.  I have a Web Browser control in Visual Basic (I am using VB 6, but have access to newer versions if needed) and would like to get the return value of a Javascript function (contained in a document loaded in the web browser control) back to VB.  I have looked at execScript, with something like:

web1.Document.parentWindow.execScript "test()", "javascript"

which does successfully execute the test function in web1's current document.  However, as indicated on the MSDN reference for execScript, getting the return value of the function is not possible. Unfortunately, I do not have access to modify the web page in question, so any manipulation on that side of things is not possible (such as having the function store the result in a global, and then pulling that variable back to VB).

I have read an interesting article "How To Call a Script Function from a VC WebBrowser Application" at
http://support.microsoft.com/kb/q185127/.  The code in the article does exactly what is needed, but the example is in Visual C++ and performing something similar in VB, if it's even possible, is beyond me.

Any help would be much appreciated.

Thank you,
Matt
0
Comment
Question by:MatteusX
  • 2
5 Comments
 
LVL 2

Expert Comment

by:YSkelling
ID: 12366286
Sorry, but you could compile this C++ function in a dll, then call it from your program ? ( With an EXPORT )

( easy answer... but simple )

Next info :

You can add a reference to mshtml.tlb in you vb application, the try to do what the C++ example does ?

Yan.
0
 

Author Comment

by:MatteusX
ID: 12377828
I do not have experience making DLLs, but it is a good idea.  I would like to stick with an all VB solution.  Thank you for the suggestion.  

I actually just solved it myself.  In my problem I mentioned storing a result of a function in a global variable.  At first I thought since I didn't have access to the javascript, I couldn't set a global.  But, this works, even though it's not very elegant:

---
'web is my webbrowser object.

web.Document.parentWindow.execScript "eval('x=test2()')", "javascript"
x = web.Document.parentWindow.x
msgbox x
---

The test2 function in the document loaded in the webbrowser control is something like:

function test2() {
   return 123;
}

This works for me.  Thank you for your suggestions.
0
 
LVL 2

Expert Comment

by:YSkelling
ID: 12382237
He found it, he keeps the points !
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12414377
The question has been PAQ'd and the 350 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question