3

Given an HTML document like this, hosted inside a WebView, how do I catch click events so they can be handled by my code, rather than the WebView?

<div onclick="location.href='http://www.blah.com/blah';">

The HTML is provided from another source, so it is desirable to to handle click events on the HTML without altering the HTML document as I cannot make assumptions about its structure.

2 Answers 2

4

You can use the addJavaScriptInterfaceMethod() in WebView.

Also more information can be found here in the Binding JavaScript code to Android code section.

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

Comments

2

You need to add a JavaScript interface (addJavascriptInterface) to your Webview and you can then call Java objects from your javascript code.

Check this:

http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)

You then create a function that is called on click, calling your registered Java object.

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.