Home > Web Front-end > JS Tutorial > javascript sets cookie validity period and detects cookies

javascript sets cookie validity period and detects cookies

陈政宽~
Release: 2017-06-28 12:00:37
Original
1541 people have browsed it

This article mainly introduces JS settings Cookie Related information about validity period detection cookies. Friends who need it can refer to

Setting cookies Function Directly upload the code:

function setCookie(name, value, days) { //设置cookie
     var d = new Date();
     d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
     var expires = "expires=" + d.toUTCString();
     document.cookie = name + "=" + value + "; " + expires;
    }
Copy after login

Set cookies:

setCookie("key", 'value', 1);
Copy after login

Cookie detection:

document.cookie.indexOf("key")
Copy after login

Return value -1 means that this cookie does not exist

The above is the JS that the editor introduces to you to set cookies and detect cookie validity period. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time. I would also like to thank everyone for your support of the Script House website!

The above is the detailed content of javascript sets cookie validity period and detects cookies. For more information, please follow other related articles on the PHP Chinese website!

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