Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Javascript function inside an webview on nativescript?

Tags:

nativescript

How to call a Javascript function (and register a callback function to be executed after the method execution from web view is done) inside an webview on nativescript?

like image 859
Richard Lee Avatar asked Sep 14 '25 16:09

Richard Lee


1 Answers

First the bad new, nothing is "built" in to do this. This requires you to add some code to communicate back from the webview. In the nativescript-webworkers plugins that I wrote; the way I did it was I used the onJsConfirm function on Android. On iOS I used the userContentControllerDidReceiveScriptMessage callback to communicate back. If you want to develop your own code; just grab a copy of my webworkers plugin and you can see the way to do it.

Now to the good news, is that it appears there is already a plugin called "nativescript-webview-interface" which gives you this facility so you don't have to build all that binding code yourself. It looks like all you need to do is include it and you will have bi-directional communications with your webview on both iOS and Android.

Note; to see if a plugin exists in the NativeScript community; your best bet is to look on http://plugins.nativescript.rocks, that is how I found out about the above listed plugin.

like image 167
Nathanael Avatar answered Sep 17 '25 19:09

Nathanael