Javascript does not work in Webview

Asked

Viewed 781 times

0

I have this simple search engine, works on all browsers, even on android Chrome, but does not work on Webviews, any suggestion of what might be and if there is any other compatible code that can replace this, target and action for example, and how to enable this for webview??

<form align="center" method="GET">
        <input type="text" placeholder="Digite Sua Pergunta Aqui" autofocus name="query" size="50">
        <input  type="submit" onclick="myFunction()" value="Buscar">
    </form>
        
    <script>
    function myFunction() {
      var query = document.getElementsByName('query')[0];
      window.open("endereco_site" + query.value);
    }
    </script>

  • 1

    You are enabling Javascript in Webview?

  • What behavior do you expect? I mean, where will this window open? will exit the app and go to the browser?

  • @Valdeirpsr in the case is the user who will have to enable this?

  • @Miguelsilva No, you are the developer. For Javascript to work, the developer must assign the value true in the method webView.setJavaScriptEnabled (in the case of Android). And how you are using popup, maybe there is also why (I did not test something like).

  • @Davidschrammel is an app that opens a page using webview, when the user types the question and clicks to search it opens another page with the search result, only webview instead of appearing the text field and Submit appear two fields followed by java script code like this: function myFunction() {&#xA; var query = document.getElementsByName('query')[0];&#xA; window.open("endereco_site" + query.value);&#xA; }

  • @Valdeirpsr got it right

  • You can do this without javascript?

  • @Miguelsilva Using modal

Show 3 more comments

1 answer

3


I believe that to enable Javascript you have to define the WebChromeClient:

webView.setWebChromeClient(new WebChromeClient());

And then enable Javascript

webView.getSettings().setJavaScriptEnabled(true);

Example:

package foo.bar.baz; //nome do seu pacote, isso é apenas um exemplo

import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity
{
    private WebView meuWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        meuWebView = (WebView) findViewById(R.id.webView); //Busca o seu webView (se estiver layout)
        meuWebView.getSettings().setJavaScriptEnabled(true);
        meuWebView.setWebChromeClient(new WebChromeClient());
    }
}

As an additional, you asked:

You can do this without javascript?

There is, using the target= and action= in the form, for example:

<form action="http://endereço" align="center" method="GET">
    <input type="text" placeholder="Digite Sua Pergunta Aqui" autofocus name="query" size="50">
    <input  type="submit" value="Buscar">
</form>
  • 1

    Very Show, worked perfectly!

  • if I use a target="_Blank" will it work in webviews? or will I have problems? I refer to the option only with action and target

  • @Miguelsilva ah truth, I think not, I will retract and improve the question, I think that for both cases it will be necessary to implement something more. I will test and as soon as possible I return.

  • I tested and worked using the target and action, so you don’t need to make changes to the APP. Thank you.

  • 1

    @Miguelsilva then has to use the onCreateWindow i’m preparing an example, I haven’t been able to create one yet (is that I always try to test my examples before posting) because some things have changed in my work and I’m overwhelmed :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.