Welcome Guest Search | Active Topics | Sign In | Register

Null Object when handling JSExtInvoke Options
John Lewis
Posted: Thursday, February 10, 2022 4:43:02 AM
Rank: Newbie
Groups: Member

Joined: 2/10/2022
Posts: 2
I'm having trouble invoking a JS function while handling a JSExtInvoke.

In my html that's loaded into WebView1:-

<button onclick=eoapi.extInvoke("DoSomething")>Do Something</button>

The vb.net code to handle it:-

Code: Visual Basic.NET
Private Sub WebView1_JSExtInvoke(sender As Object, e As JSExtInvokeArgs) Handles WebView1.JSExtInvoke
    Select Case e.FunctionName
      Case "DoSomething"
'do various checks within vb and then invoke a function based on the results
        Webview1.InvokeFunction("doSomethingElse", New Object() {}) '<----  System.NullReferenceException here
    End Select
  End Sub


'Webview1.InvokeFunction("doSomethingElse", New Object() {})' is giving me a System.NullReferenceException.

On inspection, both WebView1 and sender are null.

Am I doing something stupid?
eo_support
Posted: Thursday, February 10, 2022 11:14:03 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

You did not do anything wrong. This is a known restriction introduced in newer builds. Newer version of EO.WebBrowser no longer allow you to make synchronously JavaScript calls when you are already inside a JavaScript callback (inside JSExtInvoke handler). Allowing so would cause JavaScript engine to be re-entered, which is not allowed by design by the JavaScript engine.

The following demonstrates the danger of re-entering. Assuming JavaScript code as:

Code: JavaScript
var i = 0;
eoapi.extInvoke("something");
if (i != 0)
   alert("How did 'i' change?");


Code: Visual Basic.NET
Private Sub WebView1_JSExtInvoke(sender As Object, e As JSExtInvokeArgs)
    WebView1.EvalScript("i = 1;")
End Sub


The above code if allowed would display "How did 'i' change?" message. This is not logical from JavaScript code writer point of view. In reality re-entering can cause far more damage --- it can corrupt JavaScript engine's internal runtime data as the engine is not designed to allow re-entering.

The solution to this issue is to call your code asynchronously. You can either do it with WebView.QueueScriptCall, or you can use a feature related to your platform. For example, if you use Windows.Forms, you can use Control.BeginInvoke to delay the call.

Please feel free to let us know if you have any more question.

Thanks!
John Lewis
Posted: Thursday, February 10, 2022 11:18:50 AM
Rank: Newbie
Groups: Member

Joined: 2/10/2022
Posts: 2
Thank you for your reply! I have now managed to achieve what I want to do using WebView.QueueScriptCall which works very well (once I'd figured out how to use it).
eo_support
Posted: Thursday, February 10, 2022 11:35:11 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Great! Please feel free to let us know if there is anything else.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.