4

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?

3
  • The question is not precise enough to understand exactly what you are wanting. Are you wanting to call a JS function inside the WebView; or are you wanting to call a JS function back in the NativeScript runtimes from inside the WebView? Commented Jun 14, 2016 at 15:44
  • "...or are you wanting to call a JS function back in the NativeScript runtimes from inside the WebView?" That´s it @Nathanael ! Inside my loadStarted event i want to call a JS function that resides inside my webview, and execute a callback function... Commented Jun 14, 2016 at 15:48
  • Inside my sample.component.ts (ns-angular2): loadStarted(event: LoadEventData) { //I want call a JS funcion here and attach a callback to it. //Pseudo-code (from electron): webview.executeJavaScript("returnTokens();", false, function(returnedData: any){ SecurityContextInitializer.InitializeWithTokens(returnedData); }); ... } Commented Jun 14, 2016 at 15:53

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.