Set cookie (with JS) for whole domain not specific page

你。 提交于 2019-12-05 04:04:04

问题


I have a simple little script which I am using to set a cookie:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

The problem I have this cookie is only set on one page, not across the whole domain.

How can I adjust this function so that the cookie remains across the whole domain?


回答1:


You can specifiy domain ;domain=.example.com as well as path ;path=/ ("/" set cookie in whole domain)

document.cookie = cname + "=" + cvalue + "; " + expires +";path=/";


来源:https://stackoverflow.com/questions/26021281/set-cookie-with-js-for-whole-domain-not-specific-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!