WebBrowser OLEObject ExecuteJavascript execScript InvokeScript Xojo

I’m using a custom OLEContainer which holds a WebBrowser control (basically, I’m trying to re-create a Xojo HTMLViewer).

  • In the OLEContainer instance on the window, ProgramID is set to “Shell.Explorer”
  • I can load a HTML URL just fine by calling:
  OLEContainer1.content.navigate "http://example.com"
  • What I can’t seem to figure out is how to execute Javascript after the page is loaded.

Microsoft Documentation WebBrowser.InvokeScript Method (System.Windows.Controls) | Microsoft Docs suggests it’s a simple call to InvokeScript.

I’ve tried all sorts of variations, but it either seems to fail silently, or I get an OLEException with the “Unkown Name” exception.


dim scriptName as string = "foobar"
dim parms() as Variant
parms.append "foo"
parms.append "bar"

dim doc as OLEObject = OLEContainer1.content.Document

// None of these work:

doc.InvokeScript(scriptName, parms)   // this uses Operator_Lookup to get the InvokeScript function
dim result as variant = doc.Invoke("InvokeScript", parms)  // this uses the Xojo OLEObject.Invoke function directly

Possible Issues:

Anyone solved this?

Wow, I was totally on the wrong path - the solution is rather simple (once you have waded through a billion conflicting examples and documents). The answer is to use the execScript function which is a property WebBrowser.Document.Script …

Public Sub ExecuteJavascript(extends webBrowser as OLEContainer, script as string)
  #if TargetWin32
    
    // see https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752127(v=vs.85)
    // see https://docs.microsoft.com/en-us/previous-versions//aa752116(v=vs.85)
    // see https://stackoverflow.com/questions/5739997/how-to-execute-javascript-using-the-mshtml

    dim doc as OLEObject = webBrowser.content.Document
    dim scriptOLE as OLEObject = doc.Script
    scriptOLE.execScript(script,"javascript")
    
catch ee as OLEException
  system.debugLog CurrentMethodName + " caught OLEException " + ee.message + " when calling execScript with script '" + script + "'"
  #endif
End Sub