Sometimes we need to use cookies to save user names and record login status. How to correctly determine whether the user cookie exists on this machine? You cannot simply use a!=".
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").html(a);
}
The correct method is: to determine whether there is a cookie named username3, use document .cookie.indexOf(“username3=") to determine, if the return value is -1, it means it does not exist.
JS save and delete cookie operations
It is more convenient to use js to save and delete cookies. You don’t need to write it in the program, and it is easy to change. The more complete method of using js to save and delete cookies is as follows: