Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js reading and writing (deleting) Cookie instances_javascript skills

WBOY
Release: 2016-05-16 17:36:33
Original
1280 people have browsed it
Copy code The code is as follows:

//JS cookie operation method!
//Write cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() Days*24*60* 60*1000);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
//Read cookies
function getCookie(name )
{
var arr,reg=new RegExp("(^| )" name "=([^;]*)(;|$)");
if(arr=document.cookie .match(reg)) return unescape(arr[2]);
else return null;
}
//Delete cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name "= " cval ";expires=" exp.toGMTString();
}
//Usage example
setCookie("name","hayden");
alert(getCookie("name")) ;
//If you need to set a custom expiration time
//Then replace the above setCookie function with the following two functions;
//Program code
function setCookie2(name, value ,time){
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() strsec*1);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
function getsec(str){
alert(str);
var str1=str.substring( 1,str.length)*1;
var str2=str.substring(0,1);
if (str2=="s"){
return str1*1000;
}else if (str2=="h"){
return str1*60*60*1000;
}else if (str2=="d"){
return str1*24*60*60*1000 ;
}
}
//This is an example of setting the expiration time:
//s20 represents 20 seconds
//h refers to hours, such as 12 hours :h12
//d is the number of days, 30 days: d30
//Only these three are written for now
setCookie2("name2","hayden2","s20");
alert (getCookie("name2"));

The following are some common and useful functions:
Copy code The code is as follows:

function GetCookieVal(offset)
//Get the decoded value of the Cookie
{
var endstr = document.cookie.indexOf ( ";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//Set Cookie value
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime (expdate.getTime() ( expires * 1000 ));
document.cookie = name "=" escape (value) ((expires == null) ? "" : ("; expires=" expdate.toGMTString() ))
((path == null) ? "" : ("; path=" path)) ((domain == null) ? "" : ("; domain=" domain))
(( secure == true) ? "; secure" : "");
}
function DelCookie(name)
//Delete Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name "=" cval "; expires=" exp.toGMTString();
}
function GetCookie(name)
//Get the original value of Cookie
{
var arg = name "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i alen;
if (document.cookie.substring( i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) 1;
if (i == 0) break;
}
return null;
}
//Test
SetCookie("sunshine","1986");
alert(GetCookie("sunshine"));
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!