我希望我的索引页面(在Phonegap中)检查是否有id为> = 1的用户,然后转发到登录页面。我认为页面在数据库准备好之前加载,因此重定向不起作用。我如何让它工作?
function renderTodo(row) {
pass = row.todo;
ID = row.ID
if (ID >= 1) {
window.location.href = "login.html";
}
else {
window.location.href = "#page1";
}
}
答案 0 :(得分:1)
您应该按照以下方式从SQLResultSet中检索ID的值,并根据结果执行操作。
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
var len = results.rows.length;
for (var i=0; i<len; i++){
if(results.rows.item(i).id >= 1) {
window.location.href = "login.html";
}
else {
window.location.href = "#page1";
}
}
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);