0

I use the script from user4822973 in Post localStorage object is undefined in IE. But I still have the problem in the IE-11, when I re-write the storage, all data is null. With Firefox and Opera there is no problem.

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

}
Community
  • 1
  • 1
Wimpy
  • 1
  • `localStorage` is meant to be a *web* technology. You can't expect to work flawlessly only because your browser allows you to load local files. Can't you just run a web server locally? – MaxArt Oct 25 '16 at 12:50
  • Still not clear what your issue is. So you write the data and it is null? – epascarello Oct 25 '16 at 12:52
  • Quoting from their comment, have you done this step? [quote]All you need two do add file://127.0.0.1 to the trusted zones under the security tab (NOTE: make sure https check box IS not checked) add this line to the top or your script, depending on your code you may not need to unless you get could not connect to the internet.[/quote] – Michael Camden Oct 25 '16 at 13:27
  • @MaxArt I want to use it without a local WebServer. – Wimpy Oct 25 '16 at 14:13
  • @MichaelCamden I don´t have these option in my IE11 at work. – Wimpy Oct 25 '16 at 14:14
  • @epascarello First I do setItem() and then I close the IE Browser and after the first restart of the IE Browser the "myObj" is not null. After I do a re-write (setItem() again) the "myObj" is null. – Wimpy Oct 25 '16 at 14:19
  • The problem with documents loaded with the `file://` protocol is that user agents' behavior isn't specified, so you can't fully rely on standard API implementations. This is especially tricky when it comes to persistent data storage like `localStorage`. You may have noticed that you can't set cookies either on Chrome, for example. Remember that you don't need a full Apache/IIS for your needs, something like [http-server](https://www.npmjs.com/package/http-server) or [SimpleHTTPServer](https://docs.python.org/2/library/simplehttpserver.html) or even PHP's internal web server would be fine. – MaxArt Oct 25 '16 at 15:00
  • @wimpy that's strange. I just confirmed the options do exist on IE11, on Windows 8.1. Open IE, Click the 'Gear' icon in the upper right, click 'Internet options', then click the 'Security' tab. That should have the trusted sites option. – Michael Camden Oct 25 '16 at 15:58
  • @MichaelCamden the 'security tab' is for me at work not available :-( – Wimpy Oct 26 '16 at 06:53
  • Then it sounds like you will need to run a local server. I don't know of a different way around this issue. – Michael Camden Oct 26 '16 at 17:23

0 Answers0