7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android WebView にJavascriptのイベントハンドラを追加する

Last updated at Posted at 2014-11-16

ちょっと困ったのでメモ

androidのWebViewに読み込んだページにイベントハンドラをInjectして実行したい時があった。

WebView#onPageFinishedでjavascriptを読み込んだところうまくいきました。

android_Inject_keydown_event_handler_to_WebView_from_native_code
@InjectView(R.id.profile_registration_webView)
WebView mWebView;

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
                 // url to filter target page
        if (url.startsWith("http://target/page/url")) { 
                //something to do in javascript
            view.loadUrl("javascript:$('#target_element_id').keydown( function () {" +
                         "  $('html, body').animate({ scrollTop: 10000}, 300);" +
               "} );");
        }
    }
});
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?