How do I set path while saving a cookie value in JavaScript?

前端 未结 5 1032

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

5条回答
  •  再見小時候
    2020-12-02 16:08

    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" : "");  
       }
    

提交回复
热议问题