我有一个有两页的cordova应用程序,第一个有搜索字段,第二个显示第一个的结果。
displayResults
函数
function displayResults(){
var query = localStorage.getItem('search');
var db = window.sqlitePlugin.openDatabase({name:"Store-DB", location: 1});
queryDB(db,query);
}
function queryDB(db,query){
db.transaction(function(tx) {
var queryList = query.split(" ");
var sqlText = "SELECT * FROM DB WHERE item LIKE '%"+queryList[0]+"%'";
for (i = 1; i < queryList.length; i++) {
sqlText += " OR item LIKE '%"+queryList[i]+"%'";
}
tx.executeSql(sqlText, [], querySuccess);
}
);
}
function querySuccess(tx,results) {
var i;
var len = results.rows.length;
//Iterate through the results
for (i = 0; i < len; i++) {
console.log(row);
//Get the current row
var row = results.rows.item(i);
var div = document.createElement("div");
div.style.background = "gray";
div.style.color = "white";
div.innerHTML = row;
document.body.appendChild(div);
}
alert("Finished");
}
以下是第二页的代码:
<link rel="stylesheet" type="text/css" href="css/default.css">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="js/zepto.js" type="text/javascript"></script>
<script src="plugins/com.brodysoft.sqlitePlugin/www/SQLitePlugin.js" type="text/javascript"></script>
<script src="js/code.js" type="text/javascript"></script>
<script type="text/javascript">
function onLoad(){
document.addEventListener("deviceready", onDeviceReady(), false);
}
function onDeviceReady(){
displayResults();
}
</script>
</head>
<body onload="onLoad()">
<div id="taskbar">
<a href="index.html">Home</a>
<a id="username" class="toolbar_item" style="float:right;" href="#"></a>
</div>
第一次加载此页面时displayResults
工作正常,但如果您单击链接返回主页并再次加载第二页,则控制台会输出错误ReferenceError: no "undefined" has no property "openDatabase"
,换句话说, SQLite插件没有加载。但是,如果我按下displayResults
按下按钮而不是onload
它每次都有效。对这种特殊行为的解释是什么?
答案 0 :(得分:1)
似乎有时,当有更多不同库的事件处理程序时,或者根本不会触发deviceready事件或者过早触发。第二个问题导致brodysoft-plugin未正确加载并将window-object分配给window.sqlitePlugin属性,因为一个事件处理程序过早地调度了deviceready-event。事实证明,setTimeout就是here
的答案答案 1 :(得分:0)
You can use the onpageshow event for example
$('#idofyourpage').bind("onpageshow",function(){
//Do something
});