Home > Web Front-end > JS Tutorial > Javascript function code for operating cookies_javascript skills

Javascript function code for operating cookies_javascript skills

WBOY
Release: 2016-05-16 17:49:41
Original
937 people have browsed it

Simple version of javascript cookie operation

Copy code The code is as follows:

function setCookie(name, value, iDay) {
var oDate = new Date();
oDate.setDate(oDate.getDate() iDay);
document.cookie = name '=' value ';expires=' oDate;
}
function getCookie(name) {
var arr = document.cookie.split('; ');
var i = 0;
for(i=0; ivar arr2 = arr[i].split('=');
if(arr2[0] == name) {return arr2[1];}
}
return '';
}
function removeCookie(name) {
setCookie(name,'',-1);
}

javascript operation cookie encapsulated version
Copy code The code is as follows:

var cookie=new function(){
this. set=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;
};
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;
};
this.remove=function(name){
var cookieStr=name "=" escape('null') ";expires=" new Date().toGMTString();
document.cookie=cookieStr;
};
}

Keep it for later use Bar
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