如何将此cookie设置为在当前时间后一小时内到期:
document.cookie = 'username=' + value; + 'expires=' + WHAT GOES HERE?; + 'path = /';
Run Code Online (Sandbox Code Playgroud) 这是我的剧本。
<script>
function save() {
var x=document.getElementById("user").value;
var y=document.getElementById("password").value;
document.cookie=x+" "+y;
}
function write() {
document.write(document.cookie);
}
</script>
Run Code Online (Sandbox Code Playgroud)
这工作得很好,它会记住 var x 和 y 的值,并在调用函数 write() 时写入它们。当我关闭浏览器时,cookie 就会被删除,这是理所应当的。但我想设置一年的到期日期;我尝试了很多方法,但确实无法使其发挥作用。有什么帮助吗?
我正在设置 cookie 以在页面加载时启动模态。目前,模态只发生一次。
我需要它每个月重置一次,以便每个访问者每个月都会弹出一次。
是否可以修改我的代码来实现这一点,还是我需要以另一种方式做到这一点?任何帮助表示赞赏。
$(document).ready(function () {
//if cookie hasn't been set...
if (document.cookie.indexOf("ModalShown=true")<0) {
$("#newsletterModal").modal("show");
//Modal has been shown, now set a cookie so it never comes back
$("#newsletterModalClose").click(function () {
$("#newsletterModal").modal("hide");
});
document.cookie = "ModalShown=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
}
Run Code Online (Sandbox Code Playgroud)
});