我正在尝试使用WebView在Android上加载一个Web应用程序,而我的html文件中的JavaScript没有正常执行。
我创建WebView并加载主页,如下所示:
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函数
<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调用,如此
<input id="register" type="button" value="Submit" data-role="button" data-inline="true" data-theme="b" onclick="doRegister()" />
当点击按钮时,它似乎试图在home.html上调用doRegister()。当我点击按钮时,我得到Uncaught ReferenceError: doRegister is not defined at file:///android_asset/web/home.html:1
为了确认这一点,我在home.html中添加了一个JavaScript函数,它只显示一个警告,当我点击register.html中的按钮时,这确实执行了
对此问题的任何帮助将不胜感激。感谢。
更新:忘记提及,JavaScript在eclipse网页浏览器以及firefox中都可以正常运行。这是我的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>
答案 0 :(得分:4)
基本上webview无法找到函数doRegister(),该函数位于另一个未加载的html页面register.html中。试试这个
1.将register.html中的js部分移动到某个.js文件,例如func.js
2.然后从home.html中引用func.js作为
<script type="text/javascript" src="func.js"></script>
3。加载页面后,单击按钮应调用doRegister()