Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

javascript - How to store persistent data client side

I need to programmatically store data on the client side without having to transfer the data from the server on every page load. I considered generating a dynamic JavaScript file with the needed data for the current session of the user and make sure it is cached, but that seems really messy and there are a few drawbacks I can think of to such an approach.

How can I go about storing persistent data on the client side?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the Web Storage API (Window.localStorage or Window.sessionStorage). Check out this article on html5doctor for a more in-depth explanation. The Web Storage API is supported by all modern browsers at this point.

The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.
https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

As highlighted above:

There are two methods of setting and getting properties via the Window.localStorage and Window.sessionStorage API's:

  1. Access the properties directly:

    localStorage.name = 'ashes999';
    console.log(localStorage.name); // ashes999
    delete localStorage.name;
    console.log(localStorage.name); // undefined
    
    sessionStorage.name = 'ashes999';
    console.log(sessionStorage.name); // ashes999
    delete sessionStorage.name;
    console.log(sessionStorage.name); // undefined
    
  2. Use the Storage.setItem, Storage.getItem, and Storage.removeItem API methods.

    localStorage.setItem('name', 'ashes999');
    console.log(localStorage.getItem('name')); // ashes999
    localStorage.removeItem('name');
    console.log(localStorage.getItem('name')); // undefined
    
    sessionStorage.setItem('name', 'ashes999');
    console.log(sessionStorage.getItem('name')); // ashes999
    sessionStorage.removeItem('name');
    console.log(sessionStorage.getItem('name')); // undefined
    

Caveats:

  • Browsers may impose limitations on the storage capacity per origin of the Web Storage API, but you should be safe up to 5MB.
  • The Web Storage API is limited by the same origin policy.
  • Access to Web Storage from third-party IFrames is denied if the user has disabled third-party cookies in Firefox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...