In the daily operation process, it is unavoidable to use JavaScript in the user's browser to store some data locally, to achieve some can not use the server to identify functions, such as the identification of the user's second visit. Commonly used methods have Cookie,ie browser UserData, as well as Localstorage, because the UserData only IE support, its scalability is not large.
Cookie
The advantage of cookies is that the user requests server data once per request, and cookies are sent to the server with these requests, and the server scripting language such as PHP can handle the data sent by cookies, which is very convenient. But its shortcomings are also obvious, apart from what we often say, browsers on the number of cookies, the size of the limit, I think more important, with JS to the cookie operation is quite cumbersome, Browsers provide only document.cookie such an object, the assignment of cookies, access is more cumbersome in PHP, we can set the cookie by Setcookie (), $_cookie This super global array to get cookies. Here are the JavaScript operations I encapsulate and the functions to get cookies:
function Setcookie (name,value,expired,path,domain) {
var now=new Date ();
if (name==null) {
Throw "Cookie Name must not being Null";
}else if (value==null) {
Throw "Cookie Value must not being Null";
}else if (expired==null) {
Expired=0;
}
if (path==null) {
Path= "/";
}
if (domain==null) {
Domain=window.location.host;
}
Now.settime (Now.gettime () +expired*1000);
document.cookie=name+ "=" +escape (value) + "; expires=" +now.togmtstring () + ";p ath=" +path+ ";d omain=" +domain;
}
function GetCookie (name) {
var Allcookie=document.cookie;
Thiscookie=allcookie.match (name+ "=[^s]*");
Mycookie=thiscookie[0].split ("=");
A=mycookie[1].substring (0,mycookie[1].length-1);
Return unescape (a);
}
Setcookie () The first three parameters are mandatory, and the following two parameters represent the path, valid. When you want to delete a cookie, set the expiration date directly to a negative value. The GetCookie () function is used to get the cookie content, and the parameter is the corresponding cookie's name value.
From the above example can see the use of JS access and operation of cookies is more troublesome (compared to PHP and other languages);
Localstorage:
Relative to the operation of the cookie,localstorage is convenient and quick, it can be as normal object copy operation, there are corresponding APIs to assign value, get, delete, empty and other operations, such as, I want to store the host value of Www.iefans.net, the following methods can be:
Localstorage.host= "Www.iefans.net";
localstorage[' host ']= "www.iefans.net";
Localstorage.setitem ("Host", "www.iefans.net");
Get
Localstorage.host;
localstorage[' host '];
Localstorage.getitem ("host");
Delete
Localstorage.removeitem ("host");
Empty Locastorage
Localstorage.clear ();
From the above examples can be seen, localstorage operation is flexible and convenient, very easy to use. But it also has some drawbacks, the first is not automatically sent to the server with the request, if you want to send to the words, must be manually converted to cookies or through Ajax to send; 2nd, localstorage only a specific browser to use (support HTML5), And some of the old domestic browsers still occupy a large market, hindering the use of localstorage.