JavaScript无法与android WebView一起使用,未捕获ReferenceError [英] JavaScript not working with android WebView, Uncaught ReferenceError

查看:64
本文介绍了JavaScript无法与android WebView一起使用,未捕获ReferenceError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebView在android上加载网络应用,并且我的html文件中的JavaScript无法正确执行.

I am trying to load a web app on android using WebView and the JavaScript in my html files is not executing properly.

我创建WebView并像这样加载主页:

I create the WebView and load the home page like this:

web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
JavaScriptInterface jsInterface = new JavaScriptInterface(web, this);
web.setWebChromeClient(new WebChromeClient());
web.addJavascriptInterface(jsInterface, "JSInterface");
web.loadUrl("file:///android_asset/web/home.html");

加载首页后,我单击指向另一个本地页面register.html的链接,其中我的头部带有JavaScript函数

Once the home page is loaded I click a link to another local page, register.html, where I have a JavaScript function in the head

<script type="text/javascript">

    function doRegister() {
        var user = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var pass = document.getElementById("password").value;
    var confPass = document.getElementById("confirmPassword").value;
    var message = "";

    if (user.length < 6 || user.length > 16) {
        message = message + "Username must be between 6 and 16 characters.<br>";
    }
    if (pass.length < 8 || pass.length > 16) {
        message = message + "Password must be between 8 and 16 characters.<br>";
    }
    if (pass !== confPass) {
        message = message + "Password and confirm password do not match.<br>";
    }
    if (email.indexOf("@") === -1) {
        message = message + "Email address is invalid.<br>";
    }
    if (message === "") {
        message = JSInterface.register(user, email, pass);
        if (message === "") {
            window.location.href = "home.html";
        }
    }

    document.getElementById("message").style.color="red";
    document.getElementById("message").innerHTML=message;
    }
</script>

像这样的onclick按钮调用此功能

This function is called by a buttons onclick like so

<input id="register" type="button" value="Submit" data-role="button" data-inline="true" data-theme="b" onclick="doRegister()" />

似乎单击该按钮时,它正在尝试在home.html上调用doRegister().当我单击按钮时,我得到 Uncaught ReferenceError:在文件:///android_asset/web/home.html:1

It appears that when the button is clicked it is trying to call doRegister() on home.html. When I click the button i get Uncaught ReferenceError: doRegister is not defined at file:///android_asset/web/home.html:1

为确认这一点,我在home.html中添加了一个JavaScript函数,该函数仅显示警报,并且当我单击register.html中的按钮时确实执行了此操作

To confirm this, I added a JavaScript function to home.html that just displays an alert and this did indeed execute when I clicked the button in register.html

在此问题上的任何帮助将不胜感激.谢谢.

Any help on this problem would be greatly appreciated. Thanks.

更新:忘记了,JavaScript在Eclipse Web浏览器和Firefox中都可以正常运行.这是我的home.html

Update: Forgot to mention, the JavaScript executes fine in the eclipse web browser as well as firefox. Here is my home.html

<!doctype html>
<html>
<head>
    <title>Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="pagination/jquery.simplePagination.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>

<script type="text/javascript>
    function doSubmit() {
        alert("test");
    }
</script>


</head>
<body>
    <div data-role="page" id="home">
        <div data-role="header" data-theme="b">
            <h1>Home</h1>
        </div>
        <div data-role="content">
            <ul data-role=listview>
                <li> <a href = professors.html> Browse Professors </a> </li>
                <li> <a> Search Professors </a> </li>
                <li> <a> Browse Course </a> </li>
                <li> <a> Search Course </a> </li>
                <li> <a href = login.html> Login </a> </li>
                <li> <a href = register.html> Register </a> </li>
            </ul>
        </div>
        <div data-role="footer">
            <h4>footer</h4>
        </div>
    </div>
</body>
</html>

推荐答案

基本上,webview无法找到函数doRegister(),它在另一个未加载的html页面register.html中.试试这个
1.将register.html中的js部分移到某个.js文件,例如func.js
2.然后从您的home.html中将func.js引用为

Basically webview is not able to find the function doRegister() , sine that is in a different html page register.html, which is not loaded. Try this
1. Move the js portion in register.html to some .js file say func.js
2. Then refer to func.js from your home.html as

<script type="text/javascript" src="func.js"></script> 

3.页面加载后,单击按钮应调用doRegister()

3. once page is loaded, clicking on the button should call doRegister()

这篇关于JavaScript无法与android WebView一起使用,未捕获ReferenceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆