Confused about this…So I have a button that when clicked reveals a hidden div. Im trying to set a cookie onlick so that the div will show even after the page reloads but Im having no luck…
What I dont understand is how to get a value for a clicked state…
For example:
setCookie('CookieName', 'What goes here for the clicked button state value?', 'LENGTH OF COOKIE LIFE')
<html>
<head>
<script type="text/javascript" language="JavaScript">
function SetCookie() {
if(arguments.length < 2) { return; }
var n = arguments[0];
var v = arguments[1];
var d = 0;
if(arguments.length > 2) { d = parseInt(arguments[2]); }
var exp = '';
if(d > 0) {
var now = new Date();
then = now.getTime() + (d * 24 * 60 * 60 * 1000);
now.setTime(then);
exp = '; expires=' + now.toGMTString();
}
document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()
function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
var cookiename = n+ '=';
var cookiebegin = document.cookie.indexOf(cookiename);
var cookieend = 0;
if(cookiebegin > -1) {
cookiebegin += cookiename.length;
cookieend = document.cookie.indexOf(";",cookiebegin);
if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
cookiecontent = document.cookie.substring(cookiebegin,cookieend);
}
}
return unescape(cookiecontent);
} // function ReadCookie()
// —>
</script>
<style type="text/css">
#sub3 {
position: absolute;
left: 100px;
top: 200px;
background-color: #f1f1f1;
width: 180px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
display: none;
}
</style>
<script language="JavaScript">
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}
</script>
</head>
<body >
<script language="JavaScript">
function clicked() {
setVisibility(id, visibility);
setCookie() // this calls your set cookie function
return true;
}
</script>
<input id="input1" type=button value='Show Layer' onclick="clicked();">
<div id="sub3">Message Box</div>
</body>
</html>
Your function
setVisibility(id, visibility)accept 2 attributes, you need to specify values likesetVisibility('sub3', 'block')if you want to show your block.You nedd to pass arguments to your functions
SetCookie(name, value)andGetCookie(name).Also you need to show your block in page load event if function
GetCookie(name)returns value.