Running Javascript in Android WebView - onPageFinished Loop -
i having bit of trouble getting application correctly run js on page using onpagefinished method.
the code below contained within class i've created extends asynctask fetch , parse json file held elsewhere.
i able fetch json file correctly, parse data , url webview obtained , set. works loads should until attempt run js onpagefinished method.
//onpostexecute method runs when doinbackground method completed @suppresslint("setjavascriptenabled") @override protected void onpostexecute(boolean aboolean) { super.onpostexecute(aboolean); //casting webview findviewbyid doesnt explicity return value type. webview = (webview) findviewbyid(r.id.webview); //obtaining websettings of webview websettings webviewsettings = webview.getsettings(); //setting javascript enabled webviewsettings.setjavascriptenabled(true); webview.setwebviewclient(new webviewclient(){ @override public void onpagefinished(webview view, string url) { super.onpagefinished(view, url); webview.loadurl("document.getelementbyid('field_133').value = 'test';"); log.d("onpagefinished", "the page has finished loading"); } }); //obtaining first item in cellref list array - here access url data train operator. parsedurl = cellref.get(0).geturl(); //load page parsed online file webview.loadurl(parsedurl); log.d("loadurl", "now load parsed url"); }
all looking @ moment test js can correctly populate textbox once page has loaded value of "test" - however, webview appears stuck in loop of loading & refreshing (seeing repeated logcat prints of "the page has finished loading") when trying run:
webview.loadurl("document.getelementbyid('field_133').value = 'test';");
is correct way of trying inject js webview in android? apologies if there obvious missing, majority of experience lies in swift.
any appreciated.
thanks
try "javascript:" before code. use this, works perfectly:
loadurl("javascript:(function() { document.getelementsbytagname('video')[0].play(); })()");
Comments
Post a Comment