|
Rank: Newbie Groups: Member
Joined: 1/17/2014 Posts: 4
|
I'm trying to call javascript function from VB using WebView.EvalScript("test('123')") Bur it isn't working. I'm Following this procedure :
1. WinForm Application start. 2. On Form Load -> Loding Url into webview 3. On window.onload JavaScript Function -> Call back to vb to get data 4. Vb gets data from data source and call javaScript function with data, but at this point it's not able to call javascript function. 5. It fails without any error 6. If I add 1 milisecond VB timer interval after getting data from data source and before calling javascript function, it works. 7. Can anyone tell me please, why it don't work without 1 second timer.
VB form load :
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load MainWebView = MainBrowser.WebView MainWebView.Url = EngineV4Path AddHandler MainWebView.JSExtInvoke, New JSExtInvokeHandler(AddressOf MainWebView_JSExtInvoke) End Sub
JavaScript :
$(window).load(function () { EO.extInvoke("loadData", []); });
Again VB :
Private Sub MainWebView_JSExtInvoke(sender As Object, e As JSExtInvokeArgs) Select Case e.FunctionName Case "loadData" MsgBox("It works till this point and display me this MsgBox") MainWebView.EvalScript("alert('And Never show me this javascript alert')")
End Select End Sub
But if I change my VB code and add a 1 milisecond timer then :
Dim ttimer As New Timer Private Sub MainWebView_JSExtInvoke(sender As Object, e As JSExtInvokeArgs) Select Case e.FunctionName Case "loadData" MsgBox("It works till this point and display me this MsgBox") ttimer.Interval = 1 AddHandler ttimer.Tick, AddressOf TimerExecSub ttimer.Start()
End Select End Sub
Public Sub TimerExecSub() ttimer.Stop() MainWebView.EvalScript("alert('And Never show me this javascript alert')")
End Sub
Then it work perfectly.
Any help regarding this will be valuable. Thanks :)
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
That's the correct behavior. You can not call any JavaScript code inside a JavaScript callback function. To the JavaScript engine, your "extension function" is a single function call and the JavaScript engine does not allow other code to execute before that function returns (for example, it is not possible for you to execute other code while window.alert is blocking). As such we have code to explicitly detect this case and fail EvalScript. Using a timer to delay the execution is the correct behavior. However I think it would make sense for the product to throw a helpful error message in this case so that you will know why.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 1/17/2014 Posts: 4
|
Thanks for your help.
Is there any alternative to perform same operation without timeout? My web app works in this way.
1. User action on HTML/JS elements 2. JS request VB for data 3. VB get data from data source and send back to JS for further process on data.
So, I need something that I can enable 2-way communication with single event (click on HTML Button for example).
User -> Action/Event on HTML/JS Element -> JS -> VB -> DataSource -> VB -> JS -> HTML -> User
Regards,
S
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
You can have two ways communication but re-entry on the JavaScript side is not allowed. For example, VB -> JavaScript -> VB -> JavaScript is not allowed because the last leg causes an re-entry on the JavaScript side. Note that re-entry on the VB side is fine. But as long has you have "JavaScript -> VB -> JavaScript" in your calling chain then you have to break that. Traditionally, JavaScript does not allow re-entry so that JavaScript programmer never has to worry about re-entry issues or synchronization issues. Our product have to stick to that rule too. So if your current design requires JavaScript re-entry, then that design will not work and you have to change it.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 1/17/2014 Posts: 4
|
Thanks for your help. :)
|
|