在 JavaScript 中保存 cookie 值时如何设置路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7551113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set path while saving a cookie value in JavaScript?
提问by user959128
I am saving some cookie values on an ASP page. I want to set the root path for cookie so that the cookie will be available on all pages.
我在 ASP 页面上保存了一些 cookie 值。我想设置 cookie 的根路径,以便 cookie 在所有页面上都可用。
Currently the cookie path is /v/abcfile/frontend/
目前cookie路径是 /v/abcfile/frontend/
Please help me.
请帮我。
回答by Triptaminer
simply: document.cookie="name=value;path=/";
简单地: document.cookie="name=value;path=/";
There is a negative pointto it
有一个不利点吧
Now, the cookie will be available to all directories on the domain it is set from. If the website is just one of many at that domain, it's best not to do this because everyone else will also have access to your cookie information.
现在,cookie 将可用于设置它的域中的所有目录。如果该网站只是该域中的众多网站之一,最好不要这样做,因为其他人也可以访问您的 cookie 信息。
回答by GrvTyagi
For access cookie in whole app(use path=/):
对于整个应用程序中的访问 cookie(使用 path=/):
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Note:
笔记:
If you set
path=/
,
Now the cookie is available for whole application/domain. If you not specify the path then current cookie is save just for the current pageyou can't access it on another page(s).
如果您设置
path=/
,
现在 cookie 可用于整个应用程序/域。 如果您未指定路径,则当前 cookie 仅保存在当前页面上,您无法在其他页面上访问它。
For more info read- http://www.quirksmode.org/js/cookies.html(Domain and path part)
有关更多信息,请阅读- http://www.quirksmode.org/js/cookies.html(域和路径部分)
If you use cookies in jquery by plugin jquery-cookie:
如果您通过插件jquery-cookie在 jquery 中使用cookie:
$.cookie('name', 'value', { expires: 7, path: '/' });
//or
$.cookie('name', 'value', { path: '/' });
回答by TRIKONINFOSYSTEMS
document.cookie = "cookiename=Some Name; path=/";
This will do
这会做
回答by Femi
See https://developer.mozilla.org/en/DOM/document.cookiefor more documentation:
有关更多文档,请参阅https://developer.mozilla.org/en/DOM/document.cookie:
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number": sExpires = "; max-age=" + vEnd; break;
case "string": sExpires = "; expires=" + vEnd; break;
case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
}
回答by Omer
This will help....
这将有助于....
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return
c.substring(nameEQ.length,c.length);
}
return null;
}