admin管理员组

文章数量:1193817

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?

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?

Share Improve this question asked Sep 24, 2014 at 15:50 SheixtSheixt 2,62613 gold badges38 silver badges66 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 27

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

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

本文标签: javascriptSet cookie (with JS) for whole domain not specific pageStack Overflow