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

前端 未结 5 1038

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:10

    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;
    }
    

提交回复
热议问题