First, preface
In the recent work, there is a front-end classmate told me to clear Localstorage, I was at that time Localstorage completely no concept, so in the view of the relevant content, the following simple introduction. It is a common sense for iOS developers to popularize H5, please ignore it directly.
Second, Localstorage && sessionstorage
In HTML5, in order to store data on the client, HTML provides two new ways to store data on the client:
* Localstorage: No time limit for data storage.
* Sessionstorage: For a session of data storage, that is, re-open the page sessionstorage invalid.
Before, it was done with cookies, but cookies are not suitable for a large amount of data storage because they are passed by each request to the server, so the speed is not high. In HTML5, JavaScript is used to access and store data.
1, Localstorage
There is no time limit for the data stored by Localstorage. It will persist as long as it is not cleaned up. The following is a brief introduction to the relevant methods and use.
①, determine whether the browser supports Localstorage.
We only need to write the following code, and then open the Web page in the browser:
<!DOCTYPE HTML><HTML><Head> <title>Localstorage</title> <MetaCharSet= "Utf-8"></Head><Body> <Scripttype= "Text/javascript"> if(window.localstorage) {alert ("Browser Support Localstorage"); }Else{alert ("Browser does not support Localstorage"); } </Script></Body></HTML>
②, Localstorage Storage instances
<!DOCTYPE HTML><HTML><Head> <title>Localstorage</title> <MetaCharSet= "Utf-8"></Head><Body> <Scripttype= "Text/javascript"> if(localstorage.requestcout) {localstorage.requestcout=Number (localstorage.requestcout)+1; }Else{localstorage.requestcout= 1; } document.write ("(This is a refresh, even if you visit the page) the amount of traffic on this page is:"+Localstorage.requestcout+"Times"); </Script></Body></HTML>
Now open the HTML page directly in the browser. And then refresh, the results are as follows:
I refreshed it 13 times, and then the result was 13 times. When I close the page I just opened again, the result is as follows:
As you can see, the browser saved the original number of times to open the console can be seen:
③, storage/access Localstorage
Localstorage.setitem ("name", "Zhanggui"); localstorage["name"] = "Zhanggui"; var valure = Localstorage.getitem ("name") var valure = localstorage["name"];
Its storage is similar to a dictionary in iOS. This will save the name key to Value:zhanggui and then remove it.
④, Clear Localstorage
Localstorage.removeitem ("name"); Empties a localstoragelocalstorage.clear () according to the key value; Here is talking about all the localstorage emptied
2, Sessionstorage
Sessionstorage and Localstorage are similar in operation. The only difference is that localstorage each time the page is opened and Sessionstorage is initialized each time the page is opened. Just like the number of pages visited:
If you use Sessionstorage to store it, the next time you open the page, he will have 1 visits instead of accumulating the last number of requests.
Iii. iOS support for localstorage && Sessionstorage
Write a storage.html directly to test it. Here's the code:
The test is done here, and the result is support for localstorage. Refer to the code for details. When I opened it for the first time, the results were as follows:
When I reopen the app, the results are as follows:
When I removed the entire project and reinstalled it, the result was 1 times.
The way to empty Localstorage is as follows:
[Self.contentwebview stringbyevaluatingjavascriptfromstring:@ "Localstorage.clear ()"];
This will allow the localstorage to be emptied.
Sessionstorage is not introduced more, the reader can self-test.
Iv. Conclusion
So much for simple introduction. Want to be helpful to iOS developers.
iOS Development and Localstorage/sessionstorage