Javascriptcookie setting, reading, and deleting instance code _ javascript skills

Source: Internet
Author: User
Javascriptcookies are also a good article to store, retrieve, and delete instances. I have just compiled some articles about javascript cookies. I found this article is also good. I suggest you refer to it and select what you need. The main disadvantage is setting the path, you can combine your favorite friends.

The Code is as follows:


Script
Function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
Var Days = 30; // This cookie will be saved for 30 Days
Var exp = new Date (); // new Date ("December 31,999 8 ");
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getCookie (name) // The cookie function.
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;
}
Function delCookie (name) // delete a cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}

SetCookie ("xiaoqi", "3 ")
Alert (getCookie ('xiaoqi '));
Script




A very practical javascript function for reading and writing cookies

The Code is as follows:


Function GetCookieVal (offset)
// Obtain the decoded Cookie value
{
Var endstr = documents. cookie. indexOf (";", offset );
If (endstr =-1)
Endstr = documents. cookie. length;
Return unescape (events. cookie. substring (offset, endstr ));
}
Function SetCookie (name, value)
// Set the 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 ));
Events. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + expdate. toGMTString ()))
+ (Path = null )? "": ("; Path =" + path) + (domain = null )? "": ("; Domain =" + domain ))
+ (Secure = true )? "; Secure ":"");
}
Function DelCookie (name)
// Delete the Cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = GetCookie (name );
Documents. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
Function GetCookie (name)
// Obtain the original Cookie value
{
Var arg = name + "= ";
Var alen = arg. length;
Var clen = documents. cookie. length;
Var I = 0;
While (I <clen)
{
Var j = I + alen;
If (documents. cookie. substring (I, j) = arg)
Return GetCookieVal (j );
I = documents. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}




The Code is as follows:


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.