java - JavaScript injection into WebView not working on Android 2.3.6 -


the problem i'm having injecting javascript webview. have problem on android version 2.3.6 , below i'm assuming, don't have test device lower though. code works fine in android 4+ i'm not quite sure why it's failing. seems "submit" form doesn't fill out username , password field in 2.3.6 fails. main goal simulate form fill-out , submit in webview 2 edittexts user doesn't have interact webview itself. when user hits login button runs code:

    //set needs filled out in webview     string javascript = "(function(){ " +         "document.getelementbyid('user').value = '" + musername + "'; " +         "document.getelementbyid('password').value = '" + mpassword + "'; " +         "document.getelementbyid('form').submit(); " +         "})()";     //load javascript here     mwebview.loadurl("javascript: " + javascript); 

i'm setting webview settings this:

    //setup webview options/settings     ...     mwebview.getsettings().setsaveformdata(false);     mwebview.getsettings().setjavascriptenabled(true);     ... 

i've tried searching reason why wouldn't work on 2.3.6 haven't come anything. there restriction in version 2.* isn't present in 4+?

edit: button runs above code enabled once page finished loading. when button clicked above js injection run.

    @override     public void onpagefinished(webview view, string url) {         ...         //only allow user inject js when page has loaded         if(url.equals("...")             button.setenabled(true);         ...     } 

solution: i'm not sure if cause changing single quotes in js code use double quotes seemed make run. like:

    string javascript = ... + "document.getelementbyid(\"user\").value = \"" + musername + "\"; " 

there aren't restrictions i'm aware of it's not uncommon encounter issues stemming differing javascript engines in various versions of android. matter of trying different syntax achieve same result in attempt hit upon version of logic makes version of javascript happy.

including jquery , writing function using jquery syntax might solution, too. jquery seems pretty @ dealing these differences.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -