2

Hey experts, actually it's a pretty simple question...just don't know about the answer.

I have an Android app, running a WebView that loads a certain page, also part of the app.

At a given moment, i want to call a javascript function inside the WebView page, but i wanna do this outside the onPageFinished event.

Is that possible? Any ideas?

Thanks in advance.

user562228
  • 19
  • 1
  • 3

1 Answers1

5

Is that possible?

Sure. Use loadUrl("javascript:...");, where the ... is whatever Javascript you wish to run in the context of the currently-loaded Web page. It's basically the same trick that bookmarklets use.

Here is a sample project demonstrating this, by pushing a GPS location over to Javascript.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Look out for the fact that onpagefinished may fire several times for a given URL due to redirects. – Carlos Rendon Apr 07 '11 at 21:11
  • How can you be sure that the page has finished loading before running the script? – TacB0sS Aug 19 '13 at 10:49
  • @TacB0sS: As noted in the previous comment, use `onPageFinished()` on your `WebViewClient`. – CommonsWare Aug 19 '13 at 11:03
  • @CommonsWare, Thanks you for your help, I'm having a serious issue with the Android WebView and injecting the JavaScript at the onPageFinished... it seems like the onPageFinished call is been delayed: http://stackoverflow.com/questions/17995739/log-gate-mdev-action-completed-gate-m-seems-to-delay-execution-on-android?lq=1 – TacB0sS Aug 19 '13 at 11:16