JavaScript 不适用于 android WebView,Uncatch ReferenceError
JavaScript not working with android WebView, Uncaught ReferenceError
我正在尝试使用WebView在Android上加载Web应用程序,但html文件中的JavaScript无法正确执行。
我创建 Web 视图并加载主页,如下所示:
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>
此函数由按钮调用,如下所示
<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 中添加了一个 JavaScript 函数.html它只显示一个警报,当我单击寄存器中的按钮时,它确实执行了.html
对此问题的任何帮助将不胜感激。谢谢。
更新:忘了提,JavaScript在eclipse Web浏览器和Firefox中执行得很好。这是我的家.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页面寄存器.html中,未加载。试试这个
1.将寄存器中的js部分移动到.html到某个.js文件,例如func.js
2.然后从您家中将func.js称为.html
<script type="text/javascript" src="func.js"></script>
3. 页面加载后,点击按钮应调用 doRegister()
相关文章:
- 我收到此错误Uncatch TypeError:this.getElements不是一个函数
- Ember cli Uncatch TypeError: undefined 不是函数
- jQuery.ajax Uncatch SyntaxError: 意外标识符
- cordova 2.0.0 给出了 Uncatch TypeError:对象 0 没有方法 'func'
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- Uncatch TypeError:DB.find 不是 React with Meteor 1.3 中的函数
- jQuery after() uncatch SyntaxError: 意外令牌 ILLEGAL
- uncatch TypeError: undefined 不是 datepair(); 的函数(匿名函数)
- Javascript:uncatch TypeError:undefined不是一个函数
- Javascript uncatch Referenceerror,从 localStorage 检索数据时出错
- RequireJS QCodeDecoder 导出 Uncatch TypeError: 对象不是函数
- Wordpress uncatch TypeError:对象 [对象对象] 的属性 '$' 不是函数
- jquery datepicker Uncatch TypeError: Object [object Object]
- JavaScript uncatch TypeError:无法在第二次迭代时调用未定义的方法“拼接”
- require.js Uncatch TypeError: 对象 #
- Knockout.js : ObservableArrays element Observable Uncatch Ty
- “Uncatch TypeError: number is not a function” - JavaScript 对
- jquery 插件 Uncatch TypeError:对象不是一个函数
- Backbone collection.reset( data ) uncatch TypeError: undefin
- jQuery scrollTop 动画返回 Uncatch TypeError