1

i know that to run a javascript function in webview we need to load it in loadUrl(). In my program everything works fine javascript gets called when i use it in loadUrl, but instead of running javascript on the same page, loadUrl("javascript:function()") vanishes my previous page and run this javascript "function()" in a totally new blank page..

for eg. i tried to fill a form automatically using command:

view.loadUrl("javascript:document.getElementById('password').value = 'my_passoword'");

what happens is, the page which consists of ID-'password' vanishes and a new blank page generates consisting of 'my_password' only

where is the problem?

5
  • Try creating a function that sets the value of the input field and call that. Maybe Android does not like to evaluate the function in load url. Commented May 21, 2015 at 18:45
  • problem is it's not my site... I'm trying to make some kind of auto login system in my app using my college's website.. i cannot create my own function or change the website coding... Commented May 21, 2015 at 18:48
  • Why not? You can retrive the contents of the website, save jt to string, inject your JavaScript and load it in the webview Commented May 21, 2015 at 19:02
  • i think your way would work too, but it would be slow, as i have to login and navigate through two more pages and retrieve data.. as soon as possible. Commented May 21, 2015 at 19:17
  • Possible duplicate of Android WebView always returns null for javascript getElementById on loadUrl Commented Oct 10, 2017 at 5:03

2 Answers 2

2

Using loadUrl to run javascript loads different page

anonymous self-invoking function works fine.. i.e.

view.loadUrl("javascript:(function(){document.getElementById('password').value = 'sb14november';})()");

and as a loop around i think Bojan Kseneman answer will work too..

thanks to all!! :)

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

Comments

2

EDIT: This library is only for evaluating JavaScript and it created a new WebView instead of using an existing one :/

You can also try js evaluator library

jsEvaluator.evaluate("put your JavaScript code", new JsCallback() {
  @Override
  public void onResult(final String result) {
    // you get the result here (optional)
  }
});

3 Comments

what is the API support for this library
Supports Android version 3.0 (Honeycomb) and newer.
@SubrotoBiswas Sorry, this library is no good. It creates a new WebView instead of using an existing one :/

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.