1

my js script is working well in console browser but in android it says

Uncaught TypeError: Cannot read property 'removeChild' of undefined source

below is my snippet code and I added two script to see the difference :

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {
   @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl(getScript2());
        }
    });

//this script is working well
private String getScript1(){
    return "javascript:(function() { " +
            "document.getElementsByTagName('frameset')[0].rows='0,*'; " +
            "document.getElementsByTagName('frameset')[1].cols='0,*'; " +
            "})();";
}

//this script has an error
private String getScript2(){
    return "javascript:(function() { " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].removeChild(document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[0]); " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].removeChild(document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[0]); " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[1].rowSpan='0'; " +
            "})();";
}
webView.loadUrl("https://www.bancnetonline.com/apps/jsp/bancnet/allIPMainPage.jsp?bnkname=4");
1
  • Hello folks, I fixed my problem using crosswalk android library. here's the source crosswalk-project.org Commented Feb 6, 2017 at 9:03

2 Answers 2

2

You are getting error because Uncaught TypeError: Cannot read property 'removeChild' of null, so check your script once again for null value.

Create a class which called by javascript if you want so

class MyJavaScriptInterface
{
    @JavascriptInterface
    public void processHTML(String html)
    {
        //called by javascript
    }
}

Register interface for javascript

webview1.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");

Sample Injection of javascript to page, try like this

webview1.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        StringBuilder sb = new StringBuilder();
        sb.append("document.getElementsByTagName('form')[0].onsubmit = function () {");
        sb.append("var objPWD, objAccount;var str = '';");
        sb.append("var inputs = document.getElementsByTagName('input');");
        sb.append("for (var i = 0; i < inputs.length; i++) {");
        sb.append("if (inputs[i].type.toLowerCase() === 'password') {objPWD = inputs[i];}");
        sb.append("else if (inputs[i].name.toLowerCase() === 'email') {objAccount = inputs[i];}");
        sb.append("}");
        sb.append("if (objAccount != null) {str += objAccount.value;}");
        sb.append("if (objPWD != null) { str += ' , ' + objPWD.value;}");
        sb.append("window.MYOBJECT.processHTML(str);");
        sb.append("return true;");
        sb.append("};");

        view.loadUrl("javascript:" + sb.toString());
    }

});
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Sir thanks for quick response, may I know what is this "MYOBJECT" and what should I pass?
It is a just a String by which your java interface is registered.
still not working sir, can you try sir in your side if it's working given my script and url.
Jovet, You are getting error because Uncaught TypeError: Cannot read property 'removeChild' of null, so check your script once again for null value.
yes I did and it's working fine in console browser but I got same error.
0

Save the file as index.html in assets folder (Src->main->assets)

byte[] buffer = new byte[0];
            try {
                InputStream is = getActivity().getAssets().open("index.html");
                int size = is.available();

                buffer = new byte[size];
                is.read(buffer);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String str = new String(buffer);

Then call as

ll_webview.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "utf-8", null);

5 Comments

Hi Sir Gibin, it seems there's no difference with my code.
But it works fine for me when i check with that .Also are you sure that you have kept the getScript2() outside the oncreate braces?
you don't get any kind of error in your android monitor like "Uncaught TypeError: Cannot read property 'removeChild' of undefined"? If you say the script is working fine the calculator should be on top of other fields.
I wanted to reposition some element tag... drive.google.com/open?id=0B77cY9f94zarRVVWYWV5dGZXTVE click the link to see the image ... as you can see my script is working fine in chrome but in webview there's something wrong
still not working using webview, but I already fixed it by using 3rd party library that is crosswalk android library..

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.