Home > Web Front-end > JS Tutorial > Own js tool Cookie encapsulation_javascript skills

Own js tool Cookie encapsulation_javascript skills

WBOY
Release: 2016-05-16 18:47:43
Original
1092 people have browsed it

At this time, it is best for us to encapsulate the cookie operation for easy reuse
=====================

Copy code The code is as follows:

/**
Cookie class
Put this class into the js file used to use
1.add(name,value,100); Add a cookie
2.get(name);
3.remove(name);
Use case:
Cookie.add("sk","ss",3);
alert(cookie.get("sk"));
Cookie.remove("sk");
*/
var Cookie=new function(){
/ /Add cookie
this.add=function(name,value,hours){
var life=new Date().getTime();
life =hours*1000*60;
var cookieStr =name "=" escape(value) ";expires=" new Date(life).toGMTString();
document.cookie=cookieStr;
};
//Get cookie value
this .get=function(name){
var cookies = document.cookie.split(";");
if(cookies.length>0){
var cookie=cookies[0].split( "=");
if(cookie[0]==name)
return unescape(cookie[1]);
}
return null;
};
// Delete cookies
this.remove=function(name){
var cookieStr=name "=" escape('null') ";expires=" new Date().toGMTString();
document.cookie =cookieStr;
};
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template