我有一个索引页面,它调用安全脚本以查看您是否已登录。这是在pagebeforeshow事件中设置的
$(document).off('pagebeforeshow', '#index')
.on('pagebeforeshow', '#index', function() {
var pagetag = "index";
window.localStorage.setItem("keytag", pagetag);
init();
});
当此代码正在运行,然后执行该用户未登录。它会闪烁索引页面的内容一秒钟,然后进入登录页面。 哪个事件最好在页面创建之前运行,以便首先检查登录,这样我就不会获得index.html的闪存
我的init()部分是为了清晰
function security(){
$.ajax({
url: "http://m.mysite.com/ajaxResponder.php?method=security",
dataType: 'html',
complete: function(data){
if(data.responseText == 'logout'){
$(':mobile-pagecontainer').pagecontainer('change', 'account-login.html', {
transition: 'none',
changeHash: false,
reverse: true,
showLoadMsg: false
});
}else {
//stay on index page
}... continued script etc
回答此后的任何其他人。(感谢Omar的提醒)
我添加了
data-needs-auth="true"
到分页。
我添加了这段代码
$(document).on('pagebeforechange', function (event,data) {
if (data.toPage[0].id == "index" && data.toPage.attr('data-needs-auth') == 'true'){
if (!window.localStorage.getItem("login")) {
$.mobile.changePage("account-login.html", { changeHash: false });
event.preventDefault();
}
}
});