Home >Web Front-end >JS Tutorial >javascript cookies operation collection_javascript skills

javascript cookies operation collection_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:29:501104browse
Copy code The code is as follows:

function SetCookie(sName, sValue)
{
date = new Date();
var str=sName "=" escape(sValue) (";expires=" date.toGMTString()) ";path="/";
str=str.replace("2010 ","2099");

document.cookie=str;
alert("Congratulations, you have successfully blocked all ads of Script House. As long as you do not clear the cookies, you will no longer be affected by Script House. Advertising trouble! ");
//alert(unescape(document.cookie));
}

function DelCookie(name)
//Delete Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
if(cval!=null) document.cookie = name "=" cval "; expires=" exp.toGMTString() ";path="; ;
}


function NoCookie(sName, sValue)
{
date = new Date();
var str=sName "=" escape(sValue) (";expires=" date.toGMTString()) ";path="/";
str=str.replace("2010 ","2009");

document.cookie=str;
alert("You have restored to the Script House advertising version, thank you for your support of Script House!");
//alert(unescape(document.cookie));
}


The following functions are more commonly used, and Script House itself also uses them
Copy code The code is as follows:

function setCookie(name, value) //cookies setting JS
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() (expires*1000*3600*24));
}
document.cookie = name "=" escape (value) ((expires == null) ? "" : ("; expires=" LargeExpDate.toGMTString()));
}

function getCookie(Name) //cookies read JS
{
var search = Name "="
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search)
if( offset != -1)
{
offset = search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document. cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return ""
}
}

More You can refer to the next article.
Statement:
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