简体   繁体   English

从HTML5本地存储中检索值

[英]Retrieving value from HTML5 local storage

I have multiple JQM pages and in need to retrieve the active page ID i already stored and add it as a link to a button on the first page. 我有多个JQM页面,需要检索已经存储的活动页面ID,并将其添加为指向第一页上的按钮的链接。

I am trying to achieve a "continue" function that allows you to not lose your progress. 我正在尝试实现一个“继续”功能,使您不会失去进度。

In the example below I managed to store the active page ID and whenever the page is refreshed you will get an alert with the last page ID stored (first time it will alert #null cause no id is stored at the time). 在下面的示例中,我设法存储了活动的页面ID,并且每当刷新页面时,您都将收到一个带有最后存储的页面ID的警报(第一次它将发出#null警报,因为当时没有存储ID)。

It's basically ok but i can't seem to make that ID as the button link even though I tried $("#resume").attr("href", resume); 基本上还可以,但是即使我尝试$("#resume").attr("href", resume);我也似乎无法将ID用作按钮链接$("#resume").attr("href", resume);

I am storing the ID with this button : 我使用以下按钮存储ID:

<a href="#" data-role="button" onclick='storageValue();'>Store</a>

And this is the function I have so far: 这是我到目前为止拥有的功能:

    function storageValue() {
    var stored = $.mobile.pageContainer.pagecontainer("getActivePage").attr('id');
    alert("Your stored id is: " + stored);

    localStorage.setItem('stored', stored);
    checkLocalStorage();


}
var test = localStorage.getItem('stored');
var resume = "#" + test;
alert("Your stored id is: " + resume);

checkLocalStorage();

Optional: It would have been ideal if I didn't need to click a button to store the page. 可选:如果我不需要单击按钮来存储页面,那将是理想的选择。 The page should be stored automatically when becoming active. 该页面变为活动状态后应自动存储。

Here is a JSFIDDLE that will allow you to test/debug faster. 这是一个JSFIDDLE ,可让您更快地测试/调试。

PS: I don't think it's of any relevance to this but I am going to turn this into an android app using XDK. PS:我认为这无关紧要,但是我将使用XDK将其转换为android应用。

It's more practical to use pagecontainer events to store data. 使用pagecontainer事件存储数据更为实用。 In your case, use pagecontainerhide to store ID of page you're navigating to toPage . 在您的情况下,请使用pagecontainerhide来存储要导航到toPage的页面的ID。 And then, use pagecontainershow to update button's href . 然后,使用pagecontainershow更新按钮的href

In case stored ID is equal to first page's ID, the button will be hidden. 如果存储的 ID等于首页的ID,则该按钮将被隐藏。

$(document).on("pagecontainerhide", function (e, data) {
    var stored = data.toPage.attr('id');
    localStorage.setItem('stored', stored);
}).on("pagecontainershow", function (e, data) {
    if (data.toPage[0].id == "firstpage") {
        var test = localStorage.getItem('stored');
        var resume = "#" + test;
        $("#resume").attr("href", resume);
        if (test == "firstpage") {
            $("#resume").fadeOut();
        } else {
            $("#resume").fadeIn();
        }
    }
});

Demo 演示

If you want to navigate directly to last visited page, listen to pagecontainerbeforechange and alter toPage object. 如果要直接导航到上次访问的页面,请先听pagecontainerbeforechange并更改toPage对象。

$(document).on("pagecontainerbeforechange", function (e, data) {
    if (typeof data.toPage == "object" && typeof data.prevPage == "undefined" && typeof localStorage.getItem('stored') != "undefined") {
        data.toPage = "#" + localStorage.getItem('stored');
    }
});

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM