jquery mobile根据呼叫页面防止页面更改

jquery mobile根据呼叫页面防止页面更改,jquery,jquery-mobile,Jquery,Jquery Mobile,我试图根据用户当前所在的页面阻止jquery mobile中的页面更改,但我不知道data.options对象中有什么内容。因此,基本上我需要说,如果用户要index.html,调用的页面是example.html,那么就防止默认设置 $(document).bind("pagebeforeload", function (event, data) { var toPage = data.toPage; if (toPage == 'undefined') { return;

我试图根据用户当前所在的页面阻止jquery mobile中的页面更改,但我不知道data.options对象中有什么内容。因此,基本上我需要说,如果用户要index.html,调用的页面是example.html,那么就防止默认设置

$(document).bind("pagebeforeload", function (event, data) {
  var toPage = data.toPage;
  if (toPage == 'undefined') {
    return;
  } else {
    //need additional condition to see current page in the data.objections object?
  if (toPage == '/android_asset/www/index.html');
  event.preventdefault();
  }
});

实际上,您希望使用pagebeforechange事件


我在这里创建了一个示例

您实际上想要使用pagebeforechange事件


我在这里创建了一个示例

请注意,在Chrome on desktop上运行的jQM 1.3.2中,data参数的.toPage属性不是url而是一个对象,页面名称可以在data.toPage['0']中找到。我认为第一次调用是url,第二次调用是对象。每次导航都会调用此事件两次。请注意,在运行于Chrome on desktop的jQM 1.3.2中,data参数的.toPage属性不是url而是对象,页面名称位于data.toPage['0']。我认为第一次调用是url,第二次调用是对象。每次导航调用此事件两次。
$(document).bind('pagebeforechange', function(e, data) {
    var to = data.toPage,
        from = data.options.fromPage;

    if (typeof to === 'string') {
        var u = $.mobile.path.parseUrl(to);
        to = u.hash || '#' + u.pathname.substring(1);
        if (from) from = '#' + from.attr('id');

        if (from === '#page1' && to === '#page3') {
            alert('Cannot change to page 3 from page 1');
            e.preventDefault();

            // remove active class on button
            // otherwise button would remain highlighted
            $.mobile.activePage
                .find('.ui-btn-active')
                .removeClass('ui-btn-active');
        }            
    }
});