[英]How do I set path while saving a cookie value in JavaScript?
I am saving some cookie values on an ASP page.我在 ASP 页面上保存了一些 cookie 值。 I want to set the root path for cookie so that the cookie will be available on all pages.我想设置 cookie 的根路径,以便 cookie 在所有页面上都可用。
Currently the cookie path is /v/abcfile/frontend/
目前cookie路径是/v/abcfile/frontend/
Please help me.请帮我。
simply: document.cookie="name=value;path=/";
简单地说: document.cookie="name=value;path=/";
There is a negative point to it有一个负面的观点
Now, the cookie will be available to all directories on the domain it is set from.现在,cookie 将可用于设置它的域中的所有目录。 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 信息。
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=/
,如果你设置path=/
,
Now the cookie is available for whole application/domain.现在 cookie 可用于整个应用程序/域。 If you not specify the path then current cookie is save just for the current page you can't access it on another page(s).如果您未指定路径,则当前 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: '/' });
document.cookie = "cookiename=Some Name; path=/";
这会做
See https://developer.mozilla.org/en/DOM/document.cookie for 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" : "");
}
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.