JQM (jQueryMobile) Push last page out of DOM on changePage()

10,711

Not exactly knowing your markup I am guessing you are triggering changePage() manually. If that is the case, you could wire up one of the events listed here.

pageshow
Triggered on the page being shown, after its transition completes.

$('div').live('pageshow',function(event, ui){
  $(ui.prevPage).remove();
});

Example on jsfiddle.

Share:
10,711
Phill Pafford
Author by

Phill Pafford

Love development with PHP/Symfony/PHPStorm, iOS, PostgreSQL, Linux flavor Ubuntu, jQuery/Mobile, Foundation CSS, GitFlow AVH and HTML5 Personal Projects are Crypto Currencies, Home Automation, Mobile development, SMS/MMS and DIY electronics via Make and Hack A Day https://keybase.io/phillpafford https://onename.com/phillpafford #bitcoin: https://www.coinbase.com/phillpafford #DogeCoin: D67fwUKwKQQeL9pdbZmbWcevuAYW8XPqyz

Updated on June 25, 2022

Comments

  • Phill Pafford
    Phill Pafford almost 2 years

    I notice that the page that I was just viewing is still in the DOM. Is there a way to remove the last page viewed after I transition to the new page using changePage()?

    I have disabled all the back navigation as I don't want the use to go back to the last page when transitioning to the new page(s).

    Any suggestions?

    Flow of the site:

    • main page-> fill out info->submit on changePage()
    • next page-> fill out more info-> submit on changePage()
    • etc...

    Once the page has been submitted I don't need it anymore

    UPDATE:

    Here is the first page:

    <div data-role="page" 
         id="first_page" 
         data-theme="z" 
         data-backbtn="false" 
         data-url="first_page" 
         class="ui-page 
         ui-body-z">
    

    It appends something to this when adding a new page:

    <div data-role="page" 
         id="next_page" 
         name="next_page" 
         data-theme="z" 
         data-title="next_page" 
         data-url="getNextPage.php?page=next_page" 
         class="ui-page 
         ui-body-z">
    

    Calling the live() like this:

    // pageName[index] = next_page
    $('#'+pageName[index]).live('pagecreate',function(event, ui){   
        $(ui.prevPage).remove();
    

    using changePage() like this:

    $.mobile.changePage('getNextPage.php?page='+pageName[index],'slide',false,false);
    
  • Phill Pafford
    Phill Pafford about 13 years
    yep that's what I'm doing now. What is the trash part?
  • Phill Pafford
    Phill Pafford about 13 years
    updated my question w/ code example, but implementation of your suggests works in the demo but not my project. Would I pass the prevPage id/name to the remove()?
  • Mark Coleman
    Mark Coleman about 13 years
    Instead of registering the .live() against an #id, try placing the .live() against any div that is data-role="page" $('div[data-role="page"]').live('pageshow',..., and try the pageshow instead of pagecreate
  • Phill Pafford
    Phill Pafford about 13 years
    Hmm that does work in removing the prevPage but pageShow breaks my page functionality as I'm adding custom validation/page rendering of elements before the page is shown. What I'm doing is loading each page from an ajax call so only one page is in memory
  • Mark Coleman
    Mark Coleman about 13 years
    hmmm since you have an array of ids, maybe in your pagecreate simply do $('#'+pageName[index]).remove()?
  • Phill Pafford
    Phill Pafford about 13 years
    that works to remove the prevPage but now the current page doesn't load as it's using pagecreate() instead of pageshow, ugh.....
  • Phill Pafford
    Phill Pafford about 13 years
    After some more testing I found the in the method you offer that ann dialogs will trigger this event as well, remove needed code for functioning pages
  • OptimusCrime
    OptimusCrime almost 10 years
    I wish that I could upvote this twice. Thanks a billion!