73

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.

Currently the cookie path is /v/abcfile/frontend/

Please help me.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
user959128
  • 917
  • 2
  • 9
  • 11

5 Answers5

117

simply: 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. 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.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Triptaminer
  • 1,285
  • 1
  • 8
  • 8
  • I added an explanation to your awesome answer hope it is ok with you. – Kick Buttowski Oct 20 '15 at 21:03
  • Shouldn't this be with a comma?http://stackoverflow.com/questions/6774848/why-document-cookie-is-not-working <-- Nope, sorry :) My bad. – Jon Koeter Aug 11 '16 at 09:55
  • 2
    Maybe you should say "other websites at that domain" rather than "everyone else" because "everyone else" sounds like you mean the general public will have access to your cookies but they won't. – Volksman Dec 12 '19 at 04:11
  • This is wrong. There should be a white-space after the semicolon `;`: [ref1](https://tools.ietf.org/html/rfc6265), [ref2](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie). – c00000fd May 26 '20 at 02:12
  • developer.mozilla.org points out that "It is important to note that the path attribute does not protect against unauthorized reading of the cookie from a different path. It can be easily bypassed using the DOM, for example by creating a hidden – David Winiecki Feb 23 '21 at 17:29
43

For access cookie in whole app (use 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 page you can't access it on another page(s).

For more info read- http://www.quirksmode.org/js/cookies.html (Domain and path part)

If you use cookies in jquery by plugin jquery-cookie:

$.cookie('name', 'value', { expires: 7, path: '/' });
//or
$.cookie('name', 'value', { path: '/' });
GrvTyagi
  • 4,231
  • 1
  • 33
  • 40
  • On a large site with over e.g 100 000 listings/urls, failing to set path when setting some cookie on each url can cause problems, crawlers and user browsers might be flooded with cookies (one for each different path) or might not work as expected. – adrianTNT Jan 08 '19 at 19:47
9
document.cookie = "cookiename=Some Name; path=/";

This will do

5

See https://developer.mozilla.org/en/DOM/document.cookie for more documentation:

 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" : "");  
   }
Femi
  • 64,273
  • 8
  • 118
  • 148
2

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;
}
Omer
  • 534
  • 7
  • 19
  • 2
    This is an exact copy of answer on this page minus the erase function: https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Sam May 13 '19 at 07:34