Js to correctly determine the existence of a username cookie

  • 2020-03-30 01:29:53
  • OfStack

Sometimes we need to use cookie to save the user name and record the login status. How to correctly determine whether the cookie of this user exists? You can't 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 does not exist.

Related articles: