目前,用户打开应用程序时会看到登录页面。登录后,他们进入主页。我想阻止用户在登录后返回登录页面,用户只需单击手机或网络浏览器上的后退按钮即可轻松完成。
下面的方法通过阻止我返回或返回我想要的页面来做到这一点。例如,如果我将 id 标识为 #home
,它将阻止我返回主页。问题是这适用于我应用程序中的所有页面,减去我实际想要强制执行此操作的页面,即我的登录页面。当我使用登录页面的 ID 时,没有任何反应,我可以通过单击后退按钮返回登录页面。请告知我做错了什么。
JS
// triggers when leaving any page
//this doesn't work cos I am using the id of my loginpage. Other page's ids works.
$(document).on('pagebeforechange', function (e, data) {
var to = data.toPage;
if (typeof to === 'string') {
var u = $.mobile.path.parseUrl(to);
to = u.hash || '#' + u.pathname.substring(1);
if (to === '#loginForm') { //will work if I use #benefits for example.
alert('Can not transition the page!');
$.mobile.changePage("#home", {
transition: "flip"
});
e.preventDefault();
e.stopPropagation();
// remove active status on a button, if transition was triggered with a button
$.mobile.activePage.find('.ui-btn-active').removeClass('ui-btn-active ui-shadow').css({
'box-shadow': '0 0 0 #3388CC'
});
}
}
});
登录页面的 HTML
<div data-role="page" id="loginForm" data-theme="e" data-add-back-btn="false">
<header data-role="header">
<h1>Log In</h1>
</header>
<form id="form1" name="form1" method="GET" action="http://example.com">
<label for="id">Username </label>
<input type="text" name="id" id="id" />
<label for="password">Password </label>
<input type="password" name="password" id="password" />
<input type="hidden" name="rank" id="rank" value="123">
<input type="submit" name="submit" value="Login"/>
</form>
</div>
福利页面的 HTML
<div data-role="page" id="benefits" data-theme="e">
<header data-role="header">
<h1>Benefits</h1>
</header>
<article data-role="content" >
<div data-role="collapsible-set" id="benefitsList">
<!--will fill up with info from database-->
</div>
</article>
</div>
编辑:
$(document).on('pagebeforechange', function (e, data) {
var to = $.mobile.path.parseUrl(data.toPage);
if (typeof to === 'object') {
var u = to.href;
if (u === $.mobile.navigate.history.stack[0].url) {
alert('You are logged in!');
var current = $.mobile.pageContainer.pagecontainer("getActivePage");
$.mobile.pageContainer.pagecontainer("change", current);
}
}
});
最佳答案
上面的代码在除加载的第一个 页面之外的所有页面上都有效的问题是因为-默认情况下-jQM 不会更改第一个页面的 URL 中的散列。上面的代码检查是否从 URL 中检索到 hash
,因为第一页没有 hash
,它返回 false
因此不会阻止用户返回“登录页面”。
下面的代码检查 URL 并将其与加载的第一个页面的 .url
进行比较。 .url
存储在 $.mobile.navigate.history.stack
中,它有 0
索引,因为它是第一页。
jQuery 移动版 1.4
$(document).on('pagebeforechange', function (e, data) {
var to = $.mobile.path.parseUrl(data.toPage);
if (typeof to === 'object') {
var u = to.href;
if (u === $.mobile.navigate.history.stack[0].url) {
alert('You are logged in!');
var current = "#" + $.mobile.pageContainer.pagecontainer("getActivePage")[0].id;
window.location.hash += current;
return false; /* this will stop page change process */
}
}
});
jQuery 手机 <= 1.3
改变
$.mobile.navigate.history.stack[0].url
到
$.mobile.urlHistory.stack[0].url
改变
$.mobile.pageContainer.pagecontainer("getActivePage")[0].id
到
$.mobile.activePage[0].id
关于javascript - 无法阻止用户返回到上一个登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328168/