localStorage仅在代码之前发出警报时工作

时间:2016-03-11 03:24:57

标签: javascript local-storage

我正在尝试从本地存储加载一些变量。这是我的保存功能:

function saveGame(){
if (storageAvailable('localStorage')) {
localStorage.sTextDisplayed = document.getElementById("mainFrame").contentWindow.textDisplayed;
}
else {
    //error 
}

}

这是加载功能

function loadData(){
//error
//alert(localStorage.sTextDisplayed);
var currentText = localStorage.sTextDisplayed;
currentText += "<br>>Successful load";
alert(currentText);
document.getElementById("mainFrame").contentWindow.appendDisplay(currentText);

}

1 个答案:

答案 0 :(得分:0)

我希望这些方法可以帮助你

function storeObject(key, val, isJson)
{
    if(checkIsWebStorage())
    {
        if(isJson)
        {
            val = JSON.stringify(val);
        }
        //l("Save request : " + key + " ,data : " + val)
        localStorage.setItem(key, val);

    }

}
function readObject(key)
{
    return localStorage.getItem(key);
}
function removeObject(key)
{
    localStorage.removeItem(key);
}
function checkIsWebStorage()
{
    if(typeof(Storage) !== "undefined") 
    {
        //console.info('Support for localStorage/sessionStorage.')
    } 
    else 
    {
        //console.error('Sorry! No Web Storage support..')
    }
    return typeof(Storage) !== "undefined"
}