How to save and delete cookies in JS to determine whether a cookie exists _ basic knowledge

Source: Internet
Author: User
This article describes how to save and delete cookies in JS to determine whether a cookie exists. Sometimes we need to use cookies to save the user name and record the logon status, how can we determine whether the cookie exists correctly? You cannot simply use! =.

The 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:

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.