By reading the usage of the web storage Locastorage and SessionStorage, the god gods tried it for a trial, leaving a memorandum here.
In the project, if you use storage many times, you need to store a lot of data, you need to encapsulate it into a function:
(This function is written by the unknown god, if there is an infringement, please contact me ...)
Function SetStorage (key, value) {if (! Window.LocalStorage) {Alert (the browser does not support localStorage); Return false;} Else {var Storage = Window.LocalStorage ; // Write the field storage.setItem (key, value );}} Function GetStorage (key) {if (! Window.LocalStorage) {Alert (browser does not support localStorage);} Else {var storage = window.localStorage; VAR key = stor Age.getItem (key); // console .log (key); Return key;}}
SetStorage is stored in data. Key is the specified data name, which can be randomly arbitrarily, but it must be a string type, otherwise the browser automatically regards the value as the name of the key.
The first value as shown in the figure is that the key is not specified by a string, that is, no double quotation marks are added.
Remember to add dual quotes.
How to view Storage in the browser?The new version of the Chrome browser, view the location as shown in the figure:
If the function of the same storage data is called multiple times in the project, the data will change in real time. If you need to remove all the storage data:
localstorage.clear();或者sessionStorage.clear();
The principle used in the project is that which data needs to be stored, and which data is used to call the function of the digital data.
The above is the LocalStorage and SessionStorage use records (recommendation) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message, and I will reply to everyone in time. Thank you very much for your support for the VEVB Wulin website!