How can I set a cookie to expire after x days with this code I have.
So I have this code (which I did not make) which basically makes a container with an X, so when I press the X, the div container closes. However, when you refresh the page, the div container will reappear again.
How can I make it so when the person presses the X button, the person will never see the div container ever again (or for a set amount of time like 30 days) on that page?
I want to take it one step further, if possible, and make it so when the person presses the X button, he/she will never see the div container again throughout my site, as I plan on implementing the same div container throughout my site.
Hope this isn't too confusing, and that someone can help 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 at bottom of file, before </body> tag.)
Code:
<script type="text/javascript">
// I may have sorta kinda taken this function from W3Schools. :S
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (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);
}
}
}
007Julien, can you show me how to put it into the actual code though?
vwphillips, the code doesn't work at all . Nothing shows up. If I put the
<script type="text/javascript">
/*<![CDATA[*/
if (cookie('bottom_ad')){
document.getElementById("bottom_ad").style.display="none";
}
/*]]>*/
</script>
on the head however, I get the same result as my original post. Which is a container that can be X'd and nothing more. It doesn't not show for a set number of days after a person has hit X.
vwphillips : There are 11 characters with special meanings in regular expression : the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called "metacharacters" and strictly speaking you have to put an antislash before this characters in nme.
Bookmarks