0

I have a webview that wants to retrieve certain String value via a javascript function. I use a JavascriptInterface to print that value. This javascript lives inside a tag in a hosted HTML.

Then, when I call view.loadUrl("javascript:JSInterface.print(myFunction());") it just doesn't print anything.

However, I noticed that if I take the HTML source code and put it into my project assets and load it from there then the instruction works ok and print the desired value.

Are there any security reasons that disables calling javascript functions when they are loaded from Internet?

htafoya
  • 18,261
  • 11
  • 80
  • 104
  • Well there are peculiarities with javascript that's for sure. Try with view.loadDataWithBaseUrl() for the page itself or even for the script. (or something like that). – greenapps Jul 11 '14 at 19:24
  • Well in fact I'm loading it with view.loadDataWithBaseUrl() – htafoya Jul 12 '14 at 03:47

1 Answers1

0

Ok the problem is that if you use webview.loadDataWithBaseUrl() then the javascript functions and variables will be invalidated, thus you won't be able to access them with webview.loadUrl.

Using loadUrl to load the HTML works but it will remove the possibility to alter the HTML before it is loaded.

htafoya
  • 18,261
  • 11
  • 80
  • 104