How to modify jQuery mobile history Back Button behavior

后端 未结 6 1413
忘了有多久
忘了有多久 2020-12-13 21:47

I\'ll start this off with I have researched a bit, but no solution that solves what seems like it should be a simple JQM modification.

I have a wine review webapp th

6条回答
  •  旧巷少年郎
    2020-12-13 22:19

    I'd prefer not to splice/pop/push with the urlHistory. How about redirect on pagebeforechange like so:

    $(document).on("pagebeforechange", function (e, data) {
    
        var overrideToPage;
    
        // some for-loop to go through the urlHistory TOP>DOWN
        for (var i = $.mobile.urlHistory.activeIndex - 1; i >= 0; i--) {
            // this checks if # = your target id. You probably need to adapt this;
            if ($.mobile.urlHistory.stack[i].url = $('#yourPageId').attr('id')) {
                // save and break
                overrideToPage = $.mobile.urlHistory.stack[i].url;
                break;
            }
            // set new Page
            data.toPage = overrideToPage;
        }
    });
    

    This captures your back button changePage call and redirects to the page you want. You could also just set data.toPage = winelist directly of course.

    I'm only doing this with #internal pages, but it shoudn't be so hard to set this up with winelist.html etc.

    For more info, check the event page in the JQM docs

提交回复
热议问题