Javascriptcookies _ javascript skills

Source: Internet
Author: User
Recently, due to the need to control advertisements, we sorted out some operation control code for cookies on the pages of lumeng and his/her feet. 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 for the home of the feet. As long as you do not clear cookies, you will not be troubled by ads for the home of the feet! ");
// Alert (unescape (document. cookie ));
}

Function DelCookie (name)
// Delete the 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 already recovered to the ad version of the script house. Thank you for your support! ");
// Alert (unescape (document. cookie ));
}



The following functions are commonly used.

The Code is as follows:


Function setCookie (name, value) // set JS for cookies
{
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 ""
}
}


For more information, see the next article.
Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.