简体   繁体   English

在 JavaScript 中保存 cookie 值时如何设置路径?

[英]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.

相关问题 如何使用Javascript / jQuery在Wordpress主题模板上设置cookie,然后在另一个模板中显示cookie值(如果是isset) - How do I set a cookie on a Wordpress theme template using Javascript/jQuery and then display the cookie value in a different template (if isset) 设置Cookie路径JavaScript - Set Cookie Path JavaScript 如何在保留缩进的同时将 JSON 数据设置为 cookie? - How do I set JSON data as a cookie while preserving the indentation? 如何在JavaScript中使用XMLHttpRequest设置Cookie(标题)? - How do I SET a Cookie (header) with XMLHttpRequest in JavaScript? 如何使用javascript设置cookie的HttpOnly标志? - How do I set the HttpOnly flag of a cookie with javascript? 如何在Selenium Webdriver JavaScript脚本中设置Cookie? - How do I set a cookie in the selenium webdriver javascript script? 如何使用 javascript 从 cookie 创建和读取值? - How do I create and read a value from cookie with javascript? 如何为 cookie 函数添加路径? - How do I add a path to a cookie function? 如何使用JavaScript中的XMLHttpRequest更改“ Cookie”(在标头字段中)中一个cookie的值? - How do I change the value of one cookie in 'Cookie' (in the header field) with XMLHttpRequest in JavaScript? 如何使用 Javascript 在 CKEditor 中设置值? - How do I set a value in CKEditor with Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM