Skip to content Skip to sidebar Skip to footer

How To Convert An Element To A String That Can Be Stored And Restored From Localstorage

My plan is to store my elements in localStorage once they've been read once (for an online app – I figure that if I can get it to work it will speed up my app significantly). So

Solution 1:

The reason what you're doing doesn't work is that you're saving the item before it's loaded. load works asynchronously, like all (reasonable) ajax calls. The process starts when you call load, but finishes later. Your code is then continuing and grabbing what it thinks is the content and saving it, but the content isn't there yet.

Use the callback instead:

$(id).load(source, function() {
    showSource("from File " + content); //Added only for testing//so load it from the server..localStorage.setItem(local,$(id).html());
    //then save it to the local storage, so we don't have to do this again any time soon.
});

But...aren't you just reinventing the browser cache?

Post a Comment for "How To Convert An Element To A String That Can Be Stored And Restored From Localstorage"