Javascript does not work in Webview

0

I have this simple search engine, it works in all browsers, even in android chrome, but it does not work in Webviews, some suggestion of what it might be and if there is any other compatible code that can replace this, without being by javascript, using 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>
    
asked by anonymous 18.04.2018 / 20:29

1 answer

2

I think that in order to enable Javascript, you have to define 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 added question, you asked:

  

Can you do this without being by javascript?

You have yes, using target= and action= in 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>
    
18.04.2018 / 21:08