JS stores and deletes cookies to determine whether a cookie exists

Source: Internet
Author: User

Sometimes we need to use cookies to save the user name and record the login status. How can we correctly determine whether the user cookie exists? You cannot simply use! =.
Copy codeThe Code is as follows:
A = getCookie ("username3 ");
C_start = document. cookie. indexOf ("username3 = ");
If (c_start =-1 ){
$ ("# Login_form"). show ();
$ ("# Logined"). hide ();
}
Else {
$ ("# Login_form"). hide ();
$ ("# Logined"). show ();
$ ("# Ustr" pai.html ();
}
 
The correct method is to determine whether a cookie named username3 exists and use document. cookie. indexOf ("username3 =") to determine whether the cookie exists. If the returned value is-1, it indicates that the cookie does not exist.

Save and delete cookies in JS

It is convenient to save and delete cookies using js. You do not need to write or modify the cookies in the program. The method for saving and deleting cookies using js is as follows:
Copy codeThe Code is as follows:
<Script language = javascript>
// Obtain the coolie Value
Function cookie (name ){
Var cookieArray = document. cookie. split (";"); // obtain the split cookie name value pair.
Var cookie = new Object ();
For (var I = 0; I <cookieArray. length; I ++ ){
Var arr = cookieArray [I]. split ("="); // separate names and values
If (arr [0] = name) return unescape (arr [1]); // if it is a specified cookie, its value is returned.
}
Return "";
}

Function delCookie (name) // delete a cookie
{
Document. cookie = name + "=; expires =" + (new Date (0). toGMTString ();
}

Function getCookie (objName) {// obtain the cookie value of the specified name
Var arrStr = document. cookie. split (";");
For (var I = 0; I <arrStr. length; I ++ ){
Var temp = arrStr [I]. split ("= ");
If (temp [0] = objName) return unescape (temp [1]);
}
}

Function addCookie (objName, objValue, objHours) {// Add cookie
Var str = objName + "=" + escape (objValue );
If (objHours> 0) {// The expiration time is not set for the time period. When the browser is disabled, the cookie disappears automatically.
Var date = new Date ();
Var MS = objHours x 3600*1000;
Date. setTime (date. getTime () + MS );
Str + = "; expires =" + date. toGMTString ();
}
Document. cookie = str;
}

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 ();
}
</Script>

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.