使用 pagebeforechange 事件更改链接目标并将其保留在历史记录之外?

发布于 2024-12-26 05:46:00 字数 1123 浏览 0 评论 0原文

我正在开发一个使用 jQuery Mobile (jQM) 的 PhoneGap 应用程序。该应用程序具有需要对用户进行身份验证的区域。因此,我使用 jQM 的 pagebeforechange 来确定用户在查看他们请求的页面之前是否需要进行身份验证。如果是这样,我会将它们发送到登录页面。

我想让登录页面远离 jQM 的历史记录跟踪。也就是说,如果用户看到登录页面,但决定按“取消”,我希望应用程序返回到上一页,并且历史记录中有“下一页” ; “上一页”将位于历史堆栈的顶部。

以下是我处理登录页面重定向的方式:

$(document).bind('pagebeforechange', function(e, data) {
  if (typeof data.toPage !== 'string') {
    return;
  }

  if (data.toPage.match(/someRestrictedPage/)) {
    data.options.transition = "pop";
    data.options.changeHash = false;
    data.toPage = "myLogin.html";
  }
});

对于我正在执行的登录页面的取消按钮:

$loginCancelButton.bind('click', function() {
  var prevPage = $.mobile.urlHistory.getPrev();

  if (typeof prevPage !== 'undefined') {
    $.mobile.changePage(prevPage.url, {
      changeHash: false,
      reverse: true,
      transition: "pop"
    });
  }
});

但是,当我这样做时,我最终会得到一个带有三个的 $.mobile.urlHistory.stack elements:

[ {"index"}, {"login"}, {"index"} ]

如何管理拦截页面更改以在必要时重定向到登录表单,但不创建“无效”导航历史记录?

I am working on a PhoneGap application that uses jQuery Mobile (jQM). This application has areas that will require the user to be authenticated. So I'm using jQM's pagebeforechange to determine if the user needs to authenticate before viewing the page they have requested. If so, I send them to a login page.

I want to keep the login page out of jQM's history tracking. That is, if the user is presented with the login page, but decides to press "cancel," I want the application to go back to the previous page and not have a "next" page in the history; the "previous page" would be at the top of the history stack.

Here's how I am handling the login page redirection:

$(document).bind('pagebeforechange', function(e, data) {
  if (typeof data.toPage !== 'string') {
    return;
  }

  if (data.toPage.match(/someRestrictedPage/)) {
    data.options.transition = "pop";
    data.options.changeHash = false;
    data.toPage = "myLogin.html";
  }
});

For the cancel button of my login page I am doing:

$loginCancelButton.bind('click', function() {
  var prevPage = $.mobile.urlHistory.getPrev();

  if (typeof prevPage !== 'undefined') {
    $.mobile.changePage(prevPage.url, {
      changeHash: false,
      reverse: true,
      transition: "pop"
    });
  }
});

However, when I do this, I end up with a $.mobile.urlHistory.stack with three elements:

[ {"index"}, {"login"}, {"index"} ]

How do I manage intercepting page changes to redirect to a login form when necessary, but not create an "invalid" navigation history?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你的占有欲 2025-01-02 05:46:00

查看 jquery 文档 它提到 pagebeforeload事件需要停止。然后调用data.deferred(解决或拒绝)。

尝试将其更改为:

$(document).bind('pagebeforechange', function(e, data) {
    if (typeof data.toPage !== 'string') {
        return;
    }

    if (data.toPage.match(/someRestrictedPage/)) {
        e.preventDefault()
        data.options.transition = "pop";
        data.options.changeHash = false;
        data.toPage = "myLogin.html";
    }
    data.deferred.resolve(/* url */, data.options)
});

looking at the jquery docs it mentions that pagebeforeload event needs to be stopped. and then call the data.deferred (resolve or reject).

try changing it to:

$(document).bind('pagebeforechange', function(e, data) {
    if (typeof data.toPage !== 'string') {
        return;
    }

    if (data.toPage.match(/someRestrictedPage/)) {
        e.preventDefault()
        data.options.transition = "pop";
        data.options.changeHash = false;
        data.toPage = "myLogin.html";
    }
    data.deferred.resolve(/* url */, data.options)
});
卖梦商人 2025-01-02 05:46:00

最初问题的解决方案可能是这样的:

$(document).bind('pagebeforechange', function(e, data) {
if (typeof data.toPage !== 'string') {
    return;
}

if (data.toPage.match(/ your regex /gi))
{
    if (!check_login())
    {
         e.preventDefault();
         data.options.transition = "pop";
         data.options.changeHash = false;
         data.toPage = "#SignIn";
         $.mobile.changePage("#SignIn");
    }
    //data.deferred.resolve('#SignIn', data.options);
 }
});

这对我来说很好。

A solution for the initial problem could be something like:

$(document).bind('pagebeforechange', function(e, data) {
if (typeof data.toPage !== 'string') {
    return;
}

if (data.toPage.match(/ your regex /gi))
{
    if (!check_login())
    {
         e.preventDefault();
         data.options.transition = "pop";
         data.options.changeHash = false;
         data.toPage = "#SignIn";
         $.mobile.changePage("#SignIn");
    }
    //data.deferred.resolve('#SignIn', data.options);
 }
});

That worked for me just fine.

最终幸福 2025-01-02 05:46:00

VeXii 的解决方案似乎对我有用。只需删除

data.deferred.resolve('#SignIn', data.options);

e.preventDefault();

在 jquery mobile 1.3 上...

$(document).bind('pagebeforechange', function(e, data) {
    if (typeof data.toPage !== 'string') {
        return;
    }

    if (data.toPage.match(/index/)) {
        data.options.transition = "pop";
        data.options.changeHash = false;
        data.toPage = "#login";
    }
});

VeXii's solution seemed to work for me. By simply removing

data.deferred.resolve('#SignIn', data.options);

and

e.preventDefault();

On jquery mobile 1.3...

$(document).bind('pagebeforechange', function(e, data) {
    if (typeof data.toPage !== 'string') {
        return;
    }

    if (data.toPage.match(/index/)) {
        data.options.transition = "pop";
        data.options.changeHash = false;
        data.toPage = "#login";
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文