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: