Home > Web Front-end > JS Tutorial > body text

JS save and delete cookie operations to determine whether the cookie exists_Basic knowledge

WBOY
Release: 2016-05-16 17:15:38
Original
1471 people have browsed it

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!=".

Copy code 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").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:


Copy code The code is as follows:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!