0

I want execute a jquery on webview.I fill webview with a html file. and wrote in oncreate method this codes :

WebView wv=new WebView(this);
    setContentView(wv);

           WebSettings webViewSettings = wv.getSettings();
           webViewSettings.setJavaScriptEnabled(true);
           webViewSettings.setDomStorageEnabled(true);            
           wv.loadUrl("file:///android_asset/index.html"); 
    wv.loadUrl("javascript:fill()");

and add to oncreate method this attribute : @SuppressLint("SetJavaScriptEnabled")

My html file :

   <html>
    <head>
        <title></title>
        <script src="jquery.min.js"></script>
        <script >
function fill(){
$('#input1').val('123');
}
</script>
    </head>
    <body>
<input type="text"  id="input1"/>
</body>
</html>

I test my jquery code in chrome and worked . but when run app don't worked. Please advice

1 Answer 1

1

loadUrl() is asynchronous; it will return but the content will not have finished loading (including your fill() javascript method) before you try to invoke fill() on the next line. Try adding a listener for when the page has finished loading and invoke the javascript method from there:

wv.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:fill()");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I should write this listener in oncreate method ?
just put it before you do wv.loadUrl()
glad I could help :)

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.