0

重複の可能性:
前の位置から webapp を再開しますか?

ユーザーが戻ってきたときに、最後にアクセスしたページが表示されるようにします。例: ユーザーがアプリをロードし (私は PhoneGap を使用しています)、#second_page をクリックし、アプリを閉じて、もう一度アプリを開きます。#ホームページ の代わりに、すぐに #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 で「Uncaught TypeError: Cannot call method 'trigger' of undefined」エラーが発生します。

別のアイデアは、これらの線に沿って何かをすることです:

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

しかし、それはあまりエレガントではないようです。どうすればいいですか?

4

1 に答える 1

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;
});

私が間違っていたかもしれないことは何でも指摘してください:)

于 2012-10-18T09:21:17.047 に答える