Solved

Getting return value from Javascript function via webbrowser control in VB

Posted on 2004-10-20
5
3,796 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
[X]
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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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