未捕获的SyntaxError:webview中单引号的意外标识符错误

时间:2017-07-01 08:54:48

标签: javascript android webview

我将某些值传递给在webview上加载的表单,以使用JavaScript自动填充它。它完美无缺,直到任何一个字符串都有一个单引号“'”。遇到单引号时,我收到此错误:

Uncaught SyntaxError: Unexpected identifier

没有数据被填满。 自动填写表单的代码

public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                PageURL = view.getUrl();
                PageTitle = view.getTitle();
                actionBar = getSupportActionBar();
                if (actionBar != null) {
                    actionBar.setTitle(PageTitle);
                }

                actionBar.setSubtitle(PageURL);
                final String js = "javascript: " +
                        "var nameDoc = document.getElementsByName('name');" +
                        "nameDoc[0].value = '" + n + "';" +
                        "var checkOutDoc = document.getElementsByName('checkout');" +
                        "checkOutDoc[0].value = '" + btn_co + "';" +
                        "var noOFPaxDoc = document.getElementsByName('no_of_pax');" +
                        "noOFPaxDoc[0].value = '" + a + "';" + // a should be int based on ur HTML
                        "var noOFKidDoc = document.getElementsByName('no_of_kid');" +
                        "noOFKidDoc[0].value = '" + k + "';" + // a should be int based on ur HTML
                        "var noOFRoomsDoc = document.getElementsByName('no_of_rooms');" +
                        "noOFRoomsDoc[0].value = '" + r + "';" + // a should be int based on ur HTML
                        "var checkInDoc = document.getElementsByName('checkin');" +
                        "checkInDoc[0].value = '" + btn_ci + "';" +
                        "var email = document.getElementsByName('guest_email');" +
                        "email[0].value = '" + m + "';" +
                        "var resortName = document.getElementsByName('resort_name[]');" +
                        "resortName[0].value = '" + mail_list[0] + "';" +
                        "var distFrom = document.getElementsByName('distance_from[]');" +
                        "distFrom[0].value = '" + dist + "';" +
                        "var roomType = document.getElementsByName('room_category[]');" +
                        "roomType[0].value = '" + room_list[0] + "';" +
                        "var price = document.getElementsByName('package_price[]');" +
                        "price[0].value = '" + room_price_list[0] + "';" +
                        "var distance = document.getElementsByName('distance[]');" +
                        "distance[0].value = '" + distance + "';" +
                        "var ex = document.getElementsByName('excursions[]');" +
                        "ex[0].value = '" + ex + "';" +
                        "var act = document.getElementsByName('activities[]');" +
                        "act[0].value = '" + act_f + "';" +
                        "var dest = document.getElementsByName('destination');" +
                        "dest[0].value = '" + state + "';" +
                        "var days = document.getElementsByName('total_days');" +
                        "days[0].value = '" + d + "';" +
                        "javascript:(function(){" +
                        "l=document.getElementsByName('submit');" +
                        "e=document.createEvent('HTMLEvents');" +
                        "e.initEvent('click',true,true);" +
                        "l[0].dispatchEvent(e);" +
                        "})()";

                if (Build.VERSION.SDK_INT >= 19) {
                    view.evaluateJavascript(js, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {

                        }
                    });
                } else {
                    view.loadUrl(js);
                }
            }

1 个答案:

答案 0 :(得分:2)

这是因为您的字符串未被转义。当你正在进行数据直接插入JS时,计算机不知道你输入的cod和从输入字段添加的代码之间的区别。

因此,当您的用户输入一些文字时 nameDoc[0].value = '" + n + "';nameDoc[0].value = 'My name is Norbs' and I'm breaking your code';

在上面的示例中,字符串在“Norbs”之后结束,并且随后的内容无效JS脚本失败。

可能的解决方案

更改

nameDoc[0].value = '" + n + "';

nameDoc[0].value = '" + n.replace("'", "\'") + "';