Home >Web Front-end >JS Tutorial >Example code for setting cookie function

Example code for setting cookie function

零下一度
零下一度Original
2017-06-25 09:24:271419browse

Set the cookie function directly to 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;
                }

Set the cookie:

setCookie("key", 'value', 1);

Cookie detection:

document.cookie.indexOf("key")

The return value -1 indicates that this cookie does not exist

The above is the detailed content of Example code for setting cookie function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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