William T.
William T.

Reputation: 14321

Jquery Mobile Refresh Previous Page On Back Button

This code used to work amazing in JQM for removing previous pages from the DOM so that when you click "back" it would refresh the previous page's content.

$('div').live('pagehide', function(event, ui) {
    $(event.target).remove();
});

However, this broke in the latest JQuery update since "$().live" has been deprecated and I haven't been able to get it working using "on" or "bind" with updated parameters.

Has anyone found a working solution for this?

UPDATE:

I can't use "document.location" because I can't have certain pages appear in the "Back" navigation stack.

For example, if I go to a "submit page" and when it's submitted I simply issue the History.Back(); call and it takes you back to the updated "details page". I don't want them to click "Back" on the nav bar and be back on the "submit page" again, since the item cannot be submitted twice. There are reasons I don't use a dialog or popup for this but that would be too lengthy to explain.

I tried:

$(document).on("pagehide", "#PageId", function () {
      $(event.target).remove();
  });

And it doesn't work fully, it changes the URL path when I issue the History.Back(); command but the page stays on the "submit page". When I was using the "$().live" code, it worked great. Maybe "on" is not a good substitute?

Upvotes: 4

Views: 7792

Answers (2)

Travis M.
Travis M.

Reputation: 11257

William, you're forgetting the event parameter.

If you want to use it globally to refresh all previous pages on "back" you can use "div" instead of "#PageId". Simply put this in your javascript for your master page.

$(document).on("pagehide", "div[data-role=page]", function(event){
  $(event.target).remove();
});

Upvotes: 4

James Hill
James Hill

Reputation: 61793

This should work:

$(document).on("pagehide", "#[pageID]", function() {
    // your code here
});

Upvotes: 3

Related Questions