简体   繁体   中英

localStorage.setItem can't store strings of html

I used to have a script that used GM_setValue() to store a chunk of html from a page, and load it on another page. So:

var data = '<div id="test">testing</div>';
GM_setValue("test", data);
alert(GM_getValue("test", "failed"));

would alert <div id="test">testing</div> .

Recently we moved away from the GM API in favor of jQuery and HTML5, and replaced GM_setValue GM_getValue with functions like:

function $setVal(name, value){
 localStorage.setItem(name, value);
 return false;
}
function $getVal(name, notset){
 return localStorage.getItem(name) || notset;
}

The problem is now that it can no longer store strings of HTML like the GM equivs can. With the new functions in place, the example script would alert failed.

i've tried escaping with regex's, and escaping with escape() , but I'm having no luck.

Anyone know what to do?

EDIT: the awkward thing is, this actually seems to work fine. just not in the scope I need it to. I have a text box that is simply evaled as javascript on submission for testing, and when I call the function from there, it works, however chucks an error when called by the code. Thanks you for your help, but it doesn't actually seem to be a problem the same way I thought it was, sorry.

Try escaping the HTML like so

var html = '<div id=\"test\">testing</div>';

localStorage.setItem("Foo", html);

Put \\ before the " .

EDIT:

I just tried the following (in Firefox as it was open in front of me), and it works fine:

var html = '<div id="test">testing</div>';

localStorage.setItem("Foo", html);

console.log(localStorage.getItem("Foo"));

Just tried it in Chrome and it works fine.

Use encodeURI to store, then decodeURI to retrieve

localStorage.setItem('html',encodeURI('<div id="test">testing</div>'))
localStorage.getItem('html')
"%3Cdiv%20id=%22test%22%3Etesting%3C/div%3E"
decodeURI(localStorage.getItem('html'))
  "<div id="test">testing</div>"

I would implement the Storage of generic data by converting it to JSON and back. This works for everything that supports conversion to JSON (String, Number, most Objects, Boolean, etc) .

var Mem = (function (localStorage, JSON) {
    return {
        'set': function (key, value) {
            return localStorage.setItem(key, JSON.stringify(value));
        },
        'get': function (key) {
            return JSON.parse(localStorage.getItem(key));
        },
        'del': function () {
            for (var i = 0; i < arguments.length; ++i)
                localStorage.removeItem(arguments[i]);
        }
    };
}(window.localStorage, window.JSON)); // protect references

And usage

Mem.set('foo', 0);
Mem.set('bar', '0');
Mem.set('baz', false);
Mem.get('foo'); // Number 0
Mem.get('bar'); // String "0"
Mem.get('baz'); // Bool false
Mem.del('foo', 'bar', 'baz'); // cleanup

So for your example

var data = '<div id="test">testing</div>';
Mem.set('test', data);
console.log(Mem.get('test') === data); // true
Mem.del('test'); // cleanup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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