How do I set cookies for the domain?

Discussion in 'JavaScript' started by qwikad.com, May 4, 2018.

  1. #1
    I'd like to make this script set cookies for the domain. Right now if it sets cookies for, let's say, www.mysite.com/page_1/something.html those cookies are going to be useless for www.mysite.com/page_2/something.html

    Is there a way to to make this script simply set cookies for www.mysite.com regardless of what goes after the / forward?

    
    
    <script type="text/javascript">
    
      function setCookie(c_name,value,expiredays) {
            var exdate=new Date()
            exdate.setDate(exdate.getDate()+expiredays)
            document.cookie=c_name+ "=" +escape(value)+((expiredays==100) ? "" : ";expires="+exdate)
        }
    
        function getCookie(c_name) {
            if (document.cookie.length>0) {
                c_start=document.cookie.indexOf(c_name + "=")
                if (c_start!=-1) { 
                    c_start=c_start + c_name.length+1 
                    c_end=document.cookie.indexOf(";",c_start)
                    if (c_end==-1) c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                } 
            }
            return null
        }
    onload=function(){
    document.getElementById('myCheck').checked = getCookie('myCheck')==1? true : false;
    }
    
    </script>
    
    <input type="checkbox" value="1" id="myCheck" onclick="set_check();">
    
    
    Code (markup):

     
    qwikad.com, May 4, 2018 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,291
    Likes Received:
    1,698
    Best Answers:
    31
    Trophy Points:
    475
    #2
    Got it solved using this instead:

    
    
         function setCookie(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=/; domain=mysite.com"
        }
    
        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;
        }
    
    
    Code (markup):
     
    qwikad.com, May 5, 2018 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Here, try mine:
    
    var Cookies = {
    	'delete' : function(name, path, domain) {
    		this.get(name) && this.set(name, '', 0, path, domain);
    	}, // Cookies.delete
    	
    	get : function(name) {
    		return name && (name = d.cookie.match(
    			new RegExp('(^|;)\\s*' + name + '\\s*=\\s*([^;]*)')
    		) ? unescape(name[2]) : null);
    	}, // Cookies.get
    
    	set : function(name, value, time, path, domain, secure) {
    		if (
    			'undefined' == typeof time ||
    			time === false
    		) time = 34158171600000; // 6 June 3052, 10:00AM EST
    		if (Number.isInteger(time)) time = new Date(time);
    		if (time instanceof Date) time = time.toGMTString();
    		d.cookie = name + '=' + escape(value) +
    			'; expires=' + time +
    			(path ? '; path=' + path : '') +
    			(domain ? '; domain=' + domain : '') +
    			(secure ? '; secure' : '');
    	}, // Cookies.set
    
    	setAfter : function(name, value, time, path, domain, secure) {
    		this.set(name, value, time ? new Date().getTime() + time : false, path, domain, secure);
    	} // Cookies.setAfter
    	
    } // Cookies
    
    Code (markup):
    With set I pass the time in MS or as an instance of Date... OR as text. If you omit the date it assumes you want to set it "forever" (well, or at least until Wobbly day)

    I also pass all the other parameters you can set.

    My cookie "get" routine might be of interest to you though, it's a lot cleaner/smaller/simpler. One simple regex, no looping needed.

    Laugh is this REALLY is the type of thing that should be built into JavaScript but mysteriously has never even made it past the proposal stage with the ECMA. You even suggest it and the standards body goes "Well why would you need that?!?" -- even as they add endless crap to the "Math" object to account for some people being too lazy to type "1 -"
     
    deathshadow, May 13, 2018 IP
    qwikad.com likes this.
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Oops, forgot to edit in, where it says "d." it should be "document." -- but I figured you could handle that.
     
    deathshadow, May 14, 2018 IP