I used WordPress as a blog, I did a bit like the function. Design this point like the function, in order to prevent visitors constantly praise, have to make a time interval limit, first of all, my design is in the database record IP and the time of the likes, and then a think that also have to maintain the database (in fact, feel that no one to praise also made so strict what), decisively changed to put the data to local preservation
Locally stored data is first thought of as a cookie and HTML5 localstorage.
Cookie compatibility is good, but the amount of data stored is too small, localstorage storage is large, but the compatibility is not very good, after all, IE6 flow still have a lot of shares. Finally, considering that HTML5 is the way to go, and if both are used to ensure compatibility, then it is better to use cookies directly, I chose localstorage. All right, let's finish it and get to the point. HTML5 Local Storage Localstorage, Sessionstorage introduction.
In HTML5, local storage is the property of a window, including localStorage
and sessionStorage
, the same usage. But the former is always local, the latter is only accompanied by the session, the window once the end is gone. Now I can use the code to verify the difference between the two.
var _d = new Date (). GetTime (); if (window.localstorage) { localstorage.setitem (_d,_d); Sessionstorage.setitem (_d,_d);}
Put this code in the script of the page, after running in Chrome, view Localstorage and Sessionstorage, then close the page and open the open page to compare localstorage and sessionstorage changes
You can see that the Localstorage data has been increased, and the Sessionstorage page is always only one data when it is reopened.
Say the difference and then say the operation of the two, because the use of the same is the same, here take Localstorage to Example:
if (window.localstorage) {//Determine if the browser supports Localstorage localstorage.setitem ("UserName", "coolfish");//Save a data Key and value len =localstorage.length;//read the length of the localstorage data userName =localstorage.getitem ("UserName");// Read key userName data Localstorage.removeitem ("UserName");//delete key for userName data localstorage.clear ();// Clear all the data}
Another thing to note is that the data stored locally is string!
HTML5 Local storage localstorage and Sessionstorage