JavaScript uses browser local storage capabilities

Source: Internet
Author: User
Tags php and setcookie

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.


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) {



if (path==null) {

Path= "/";


if (domain==null) {;


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);


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, the following methods can be: "";

localstorage[' host ']= "";

Localstorage.setitem ("Host", "");


localstorage[' host '];

Localstorage.getitem ("host");


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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.