1

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.

5
  • 1
    Can you share more details about the platform version you are runnign and SDK you are building with? Commented Jun 23, 2014 at 11:45
  • I am building with Android 4.4.2 (API 19) and running it on a Android 4.4.3 device. Commented Jun 23, 2014 at 18:51
  • 1
    Can you try using the WebView.evaluateJavaScript API instead? Commented Jun 23, 2014 at 23:27
  • @ksasq Thank you so much for your help. This worked perfectly. Maybe on pre API 19 devices the code in my question will work, because that evaluateJavaScript API is only available in API 19. I will test this when I have some time. Commented Jun 24, 2014 at 13:43
  • 1
    Thanks for the update - glad it works for you. Yes, this is a change in the chromium webview that was introduced in Android 4.4. You should be able to use the loadUrl approach when your app is running on older devices. I'll formalise into an answer :) Commented Jun 24, 2014 at 19:36

1 Answer 1

3

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.

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

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.