Web Analytics Made Easy -
StatCounter Changing cookie duration - CodingForum

Announcement

Collapse
No announcement yet.

Changing cookie duration

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing cookie duration

    Hello!

    I've no experience with javascript and i'd like to modify a script, i need to change the cookie duration to 30 days. The cookie portion of the script is the following:

    Code:
    }
    
    function unblockContent(){
        document.getElementsByTagName('body')[0].style.overflow = blocker_originalBodyOverflow;
        document.getElementsByTagName('html')[0].style.overflow = blocker_originalHtmlOverflow;
    
        document.getElementById('blocker_haze').style.display = 'none';
        document.getElementById('blocker_centerPane').style.display = 'none';
        blocker_setCookie("content_unlocked", 1, 2000);
    }
    
    function blocker_setCookie(c_name,value,expiredays){
        var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
    
    function blocker_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 "";
    }
    By the way could anyone please tell me what is the current cookie duration on the code above?

    Thanks!!

  • #2
    function blocker_setCookie(c_name,value,expiredays){


    The number of days is set when the function blocker_setCookie is called, which you have not shown us, something like:-

    window.onload = blocker_setCookie("MyCookieName", "CookieValue", "365");

    so you need to change 365 or whatever to 30.



    Seen in the classified ads: Nice parachute - never opened - used once.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Thanks for the reply!

      I found this part of the code, which i think could be the one to modify. Is the part highlighted the one i need to change? should i change the "1" to another value.
      So, right now the cookie is set to 1 day?

      Code:
      function blocker_addLoadEvent(func)
      {    
          var oldonload = window.onload;
          if (typeof window.onload != 'function')
          {
              window.onload = func;
          } 
          else 
          {
              window.onload = function()
              {
                  oldonload();
                  func();
              }
          }
      }
      
      blocker_addLoadEvent(blocker_init);
      
      function blocker_init()
      {    
         [B] if(blocker_getCookie('content_unlocked')=='[COLOR="Red"]1[/COLOR]')
              return;[/B]
      Originally posted by Philip M View Post
      function blocker_setCookie(c_name,value,expiredays){


      The number of days is set when the function blocker_setCookie is called, which you have not shown us, something like:-

      window.onload = blocker_setCookie("MyCookieName", "CookieValue", "365");

      so you need to change 365 or whatever to 30.



      Seen in the classified ads: Nice parachute - never opened - used once.
      Thank you very much

      Comment


      • #4
        Checking the first piece of code i posted there is this part:

        Code:
        blocker_setCookie("content_unlocked", 1, 2000);
        What does 1 and 2000 mean in there?

        Isn't this part the one i should modify?


        I'm including the whole code anyways:

        Code:
        var blocker_originalHtmlOverflow;
        var blocker_originalBodyOverflow;
        
        function blocker_addLoadEvent(func)
        {    
            var oldonload = window.onload;
            if (typeof window.onload != 'function')
            {
                window.onload = func;
            } 
            else 
            {
                window.onload = function()
                {
                    oldonload();
                    func();
                }
            }
        }
        
        blocker_addLoadEvent(blocker_init);
        
        function blocker_init()
        {    
            if(blocker_getCookie('content_unlocked')=='1')
                return;
        
            blocker_originalHtmlOverflow = document.getElementsByTagName('body')[0].style.overflow;
            blocker_originalBodyOverflow = document.getElementsByTagName('html')[0].style.overflow;
        
            document.getElementsByTagName('body')[0].style.overflow = 'hidden';
            document.getElementsByTagName('html')[0].style.overflow = 'hidden';
        
            var haze = document.createElement('div');
            haze.id = 'blocker_haze';
            haze.style.filter = 'alpha(opacity=50)';
            haze.style.opacity = .5;
            haze.style.height = '100%';
            haze.style.width = '100%';
            haze.style.backgroundColor = '#000';
            haze.style.position = 'absolute';
            haze.style.top = '0px';    
            haze.style.left = '0px';
            haze.style.zIndex = 1000000;
        
            var centerPane = document.createElement('centerPane');
            centerPane.id = 'blocker_centerPane';
            centerPane.style.width = '500px';
            centerPane.style.border = '5px solid #CCC';
            centerPane.style.width = '500px';
            centerPane.style.position = 'absolute';
            centerPane.style.left = '50%';
            centerPane.style.marginLeft = '-250px';
            centerPane.style.top= '50px';
            centerPane.style.backgroundColor = '#FFF';
            centerPane.style.zIndex = 1000001;
            centerPane.style.backgroundImage = 'url(http://www.site.com/image.jpg)';
            centerPane.style.backgroundRepeat = 'no-repeat';
            centerPane.style.backgroundPosition = '20px 10px';
            centerPane.style.padding = '20px';
        
            var h1 = document.createElement('h1');
            h1.style.color = '#466805';
            h1.style.textAlign = 'center';
            h1.style.fontSize = '34px';
            h1.style.margin = '0 0 10px 0';
            h1.style.padding = '8px 0 0 15px';
            h1.style.fontFamily = 'arial';
            h1.style.lineHeight = '34px';
            h1.innerHTML = blocker_headline;
            centerPane.appendChild(h1);
        
            var p1 = document.createElement('p');
            p1.innerHTML = blocker_instructionalText;
            p1.style.textAlign = 'center';
            p1.style.padding = '20px 0 20px 0';
            p1.style.margin = '0';
            p1.style.fontSize = '18px';
            p1.style.lineHeight = '18px';
            p1.style.color = '#000';
            p1.style.fontFamily = 'arial';
            centerPane.appendChild(p1);
        
            var ul = document.createElement('ul');
            ul.style.textAlign = 'center';
            ul.style.margin = '0 0 10px 0';
            ul.style.padding = '0';
            ul.style.listStyleType = 'none';
            for(var i = 0;i<blocker_surveryLinks.length;i++){
                var li = document.createElement('li');        
                var a = document.createElement('a');
                a.style.display = 'block';
                a.style.fontSize = '16px';
                a.style.lineHeight = '22px';
                a.style.color = 'Blue';
                a.style.fontFamily = 'arial';
                a.style.textDecoration = 'underline';
                a.target = '_blank';        
                a.href = blocker_surveryLinks[i].url;
                a.innerHTML = blocker_surveryLinks[i].text;
                a.onclick = function(){
                    window.setTimeout(unblockContent, 45000);
                }
                li.appendChild(a)    
                ul.appendChild(li);
            }
        
            centerPane.appendChild(ul);
        
            var p2 = document.createElement('p');
            p2.style.textAlign = 'center';
            p2.innerHTML = blocker_footerText;
            p2.style.padding = '20px 0 0 0';
            p2.style.margin = '0';
            p2.style.color = '#000';
            p2.style.fontFamily = 'arial';
            p2.style.fontSize = '13px';
            p2.style.lineHeight = '13px';
            centerPane.appendChild(p2);
        
            document.getElementsByTagName('body')[0].appendChild(haze);
            document.getElementsByTagName('body')[0].appendChild(centerPane);
        }
        
        function unblockContent(){
            document.getElementsByTagName('body')[0].style.overflow = blocker_originalBodyOverflow;
            document.getElementsByTagName('html')[0].style.overflow = blocker_originalHtmlOverflow;
        
            document.getElementById('blocker_haze').style.display = 'none';
            document.getElementById('blocker_centerPane').style.display = 'none';
            blocker_setCookie("content_unlocked", 1, 2000);
        }
        
        function blocker_setCookie(c_name,value,expiredays){
            var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
            document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        }
        
        function blocker_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 "";
        }
        Thank you!!

        Comment


        • #5
          The function blocker_setCookie() is called as follows:-

          blocker_setCookie("content_unlocked", 1, 2000);

          Where "content_unlocked" is c_name (cookie name)
          1 is the cookie value
          2000 is expiredays (about 5.5 years)

          These parameters are then passed to the function:-

          function blocker_setCookie(c_name,value,expiredays){

          So if you want the expiry date to be 30 days, alter 2000 to 30.

          All the code given in this post has been tested and is intended to address the question asked.
          Unless stated otherwise it is not just a demonstration.

          Comment

          Working...
          X