2012-10-17 28 views
0

Possible Duplicate:
resume webapp from previous position?如何在jQuery Mobile中設置主頁?

我希望用戶在返回時顯示他們訪問的最後一頁。例如:用戶加載我的應用程序(我正在使用PhoneGap),點擊#second_page,關閉應用程序,再次打開應用程序。我不想#home頁面,而是立即向他展示#second_page。

我會使用localStorage來保存網頁:

$(document).on('pagechange', function() { 
    localStorage.setItem('lastPage',window.location.hash); 
}); 

我嘗試這樣做:

$(document).bind("mobileinit", function(){ 
    var lastPage = localStorage.getItem('lastPage'); 
    if(lastPage) { 
     $.mobile.changePage(lastPage); 
    } 
}); 

,但我得到一個「遺漏的類型錯誤:無法調用未定義的方法‘觸發’」錯誤在jquery.mobile-1.2.0.min.js中:2。

另一個想法是做沿着這些路線的東西:

var redirectedToLastPage = false; 
$(document).bind("pageinit", function(){ 
    var lastPage = localStorage.getItem('lastPage'); 
    if(lastPage && !redirectedToLastPage) { 
     $.mobile.changePage(lastPage); 
     redirectedToLastPage = true; 
    } 
}); 

,但它似乎不是很優雅。我應該怎麼做?

+0

查看這個問題我的回答:http://stackoverflow.com/問題/ 12816286 /恢復-web應用從 - 以前的位置/ 12820115#12820115 – adamdehaven

回答

0

我結束了此解決方案:

$(document).on('pagechange', function() { 
    var lastPage = '#'+$.mobile.activePage.attr('id'); 
    // console.log('setting last page to '+lastPage); 
    localStorage.setItem('lastPage',lastPage); 
}); 

var redirectedToLastPage = false; 
$(document).bind("pagecreate", function(){ 
    var lastPage = localStorage.getItem('lastPage'); 
    if(lastPage // lastPage needs to be set 
     && !redirectedToLastPage // only do it on first pageload 
     &&!window.location.hash // we don't want to redirect from pages other than the homepage 
    ) { 
     if($("div[data-role='page']"+lastPage).length) { // make sure a "page" div with that ID exists! 
      // console.log('redirecting to '+lastPage); 
      $.mobile.changePage(lastPage); 
     } 
     // else { 
     // console.log(lastPage+' does not exist!'); 
     // } 
    } 
    redirectedToLastPage = true; 
}); 

請隨時指出什麼我可能做錯了:)

相關問題