JS IE11 - 重写localStorage为null

时间:2016-10-25 12:43:10

标签: javascript html5 internet-explorer-11

我使用帖子localStorage object is undefined in IE user4822973 的脚本。 但是我仍然在 IE-11 中遇到问题,当我重新写入存储时,所有数据都为空。使用Firefox和Opera没有问题。

    // Check 'local storage'
    !localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$1$$"), (l.href = l.protocol + "//127.0.0.1" + p));

    if (typeof(Storage) != "undefined") {

        var myObj = JSON.parse(localStorage.getItem("multipleData"));
        alert(myObj);

        if (!myObj || 0 === myObj.length){
            //default values
            document.getElementById("reason").value = reason;
            document.getElementById("mailTo").value = mailTo;
            document.getElementById("mailCc").value = mailCc;
            document.getElementById("workStart").value = workStart;
            document.getElementById("workEnd").value = workEnd;
        }
        else{
            var myObjReason = myObj["reason"];
            var myObjMailTo = myObj["mailTo"];
            var myObjMailCc = myObj["mailCc"];
            var myObjWorkStart = myObj["workStart"];
            var myObjWorkEnd = myObj["workEnd"];
            ...

//Write to storage
function saveLocalStorage(){
    var multipleData = {"reason" : document.getElementById("reason").value , "mailTo" : document.getElementById("mailTo").value , "mailCc" : document.getElementById("mailCc").value , "workStart" : document.getElementById("workStart").value, "workEnd" : document.getElementById("workEnd").value};

    localStorage.setItem("multipleData", JSON.stringify(multipleData));

}

0 个答案:

没有答案