Dick Lucas
Dick Lucas

Reputation: 12639

Auto fill field in WebView using javascript fails

I am attempting to auto fill a field in a WebView. The page source of the webpage is here: http://pastebin.com/WKbj7XJh

I have javascript enabled in my WebView and I have the following code in my WebViewClient

public void onPageFinished(final WebView view, String url) {
        super.onPageFinished(view, url);
        String website = "https://www.example.com/oauth_callback";
        view.loadUrl("javascript:document.getElementById(\"application_callback_url\").value = '"+website+"';");
    }

Instead of autofilling the field, I get a blank WebView that simply displays: "https://www.example.com/oauth_callback"

I used this question as a guide: Fill fields in webview automatically

All help is greatly appreciated.

Upvotes: 1

Views: 1646

Answers (1)

ksasq
ksasq

Reputation: 4412

When you start to target API 19 and above, this is the behavior of WebView.loadUrl when it's passed a javascript: URL that returns a value.

You should update your app so that when running on API 19 or above, it uses evaluateJavaScript, and falls back to loadUrl when running on older devices. Something like:

if (android.os.Build.Version.SDK_INT >= 19) {
    webview.evaluateJavaScript("...", null);
} else {
    webview.loadUrl("javascript:...");
}

Alternatively, you could mangle your javascript into a JS function that doesn't return a value, and use that with loadUrl on all platform versions.

Upvotes: 3

Related Questions