javascript - How can I set a cookie to expire after x days with this code I have? -


possible duplicate:
what “best” way , set single cookie value using javascript

so have code (which did not make) makes container x, when press x, div container closes. however, when refresh page, div container reappear again.

how can make when person presses x button, person never see div container ever again (or set amount of time 30 days) on page?

i want take 1 step further, if possible, , make when person presses x button, he/she never see div container again throughout site, plan on implementing same div container throughout site.

hope isn't confusing, , can me out. thanks!

html code:

<div id="bottom_ad">     <div id="close_ad" onclick="close_bottom_ad();">x</div>     <!-- ad content --> </div> 

css code:

#bottom_ad {     position: absolute;     right: 0;     bottom: 0;     left: 0;     height: 80px;     background: #000; } #close_ad {     position: absolute;     top: 8px;     right: 8px;     width: 15px;     height: 15px;     color: #fff; } 

javascript (place @ bottom of file, before tag.) code:

<script type="text/javascript">     // may have sorta kinda taken function w3schools. :s     function getcookie(c_name)     {         var i,x,y,arrcookies=document.cookie.split(";");         (i=0;i<arrcookies.length;i++)         {             x=arrcookies[i].substr(0,arrcookies[i].indexof("="));             y=arrcookies[i].substr(arrcookies[i].indexof("=")+1);             x=x.replace(/^\s+|\s+$/g,"");             if (x==c_name)             {                 return unescape(y);             }         }     }      var ad_cookie = getcookie("closedad");      if (ad_cookie != null && ad_cookie != "")     {         document.getelementbyid("bottom_ad").style.display="none";     }      function close_bottom_ad()     {         document.getelementbyid("bottom_ad").style.display="none";         document.cookie = "closedad=true;";     } </script> 

to set 30 day expiration on cookie, need add expiration time cookie. here's pretty simple cookie function sets cookie n days:

function createcookie(name, value, days) {     var date, expires;     if (days) {         date = new date();         date.settime(date.gettime()+(days*24*60*60*1000));         expires = "; expires="+date.togmtstring();     } else {         expires = "";     }     document.cookie = name+"="+value+expires+"; path=/"; } 

by setting appropriate expiration date in cookie, removed automatically @ time.

then, when page loads (you have wait until page has finished loading before attempting modify it), can check see if cookie closead set , if so, execute code hide ad. more flicker free viewing experience (so ad doesn't first show open, close), can either start out ad hidden , show if there no cookie. or can dynamically add page if there no cookie.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -