HPPH
HPPH

Reputation: 35

How to change the URL of a WebView with Javascript?

I have a WebView pointed to a page that is supposed to redirect to another page, but it doesn't do anything. JavaScript is enabled on the app's side.

location.href = location.origin + location.pathname + '?v=' + data;

Upvotes: 1

Views: 991

Answers (1)

nfischer
nfischer

Reputation: 181

Have you set a WebViewClient? WebView's default behavior (if you have no WebViewClient) is:

  • If a navigation is renderer-initiated with "user gesture", create an Intent (code)
  • If a navigation is renderer-initiated without "user gesture" (e.g., created by JavaScript), drop the navigation (code)
  • If a navigation is renderer-initiated and goes to some sort of internal URL (e.g., about:blank), load it in the WebView (code)
  • If a navigation is browser-initiated (e.g., loadUrl()), load it in the WebView

You can override this default behavior by setting a default-constructed WebViewClient:

webView.setWebViewClient(new WebViewClient());

You can find more info on this developer guide.

Upvotes: 1

Related Questions