PDA

View Full Version : Cookie: Hide div on click 24hr?



ComputerSolutions
05-20-2012, 03:29 PM
I would like a simple JavaScript code that will allow me to hide a certain div element when clicked for a predefined amount of time. To be a little more informative, I have a suggestions box that appears when the home page is loaded. What I would like is when the div close button is clicked it sets a cookie to keep the box div closed for 24 hours (1day). Simply said, when the div close button is pressed, the box div is hidden for 24 hours. Note: I have a javascript that allows the close button to close the box but it will load every refresh.

http://i.stack.imgur.com/du1pA.jpg
http://pastebin.com/rwZiu6ai
:confused:

vwphillips
05-20-2012, 04:13 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function CheckCookie(nme){
if (cookie(nme)){
document.getElementById(nme).style.display='none';
}
}

function setcookie(nme,days){
document.cookie=nme+'=true;expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
document.getElementById(nme).style.display='none';
}

function cookie(nme){
var re=new RegExp(nme+'[^;]+','i');
if (document.cookie.match(re)){
return document.cookie.match(re)[0].split("=")[1];
}
return null;
}


/*]]>*/
</script>
</head>

<body>
<input type="button" name="" value="Hide" onmouseup="setcookie('nme',1)"/>
<img id="nme" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" alt="img" />
<script type="text/javascript">
/*<![CDATA[*/
CheckCookie('nme');
/*]]>*/
</script>
</body>

</html>

ComputerSolutions
05-20-2012, 04:47 PM
SOLVED - http://jsfiddle.net/FcFW2/1/