Jump to content

remember div display status in cookie


clausowitz

Recommended Posts

Hi All,

 

I have some javascript on my page that toggles the visibility of my divs.

I want to save the status of a div in a cookie for later.

The code to change the visibility is working but I cannot save it to the cookie and read

the status of all divs on loading of the page.

<script language="javascript" type="text/javascript"> 
name='open';
function toggle5(showHideDiv, switchImgTag) {
        var ele = document.getElementById(showHideDiv);
        var imageEle = document.getElementById(switchImgTag);
        if(ele.style.display == "block") {
                ele.style.display = "none";
			createCookie(name,'false',0);
			imageEle.innerHTML = '<img src="images/o.gif" title="Show this window" align="right">';
        }
        else {
                ele.style.display = "block";
                createCookie(name,'true',0);
			imageEle.innerHTML = '<img src="images/x.gif" title="Hide this window" align="right">';
        }
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function createCookie(name,value,days) {
if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function checkCookie(name) {
    var x = readCookie(name)
    if (x == 'false') {
    	document.getElementById('showHideDiv').style.display = 'none'
    } else if (x == 'true') {
        document.getElementById('showHideDiv').style.display = 'block'
    }
}
</script>
<body onload="javascript:checkCookie('open')">

Link to comment
Share on other sites

I made a few changes, but this works great. I added a body to show the display and I did change that, because I don't have your image. and added to the checkCookie(name) function.

<script type="text/javascript">
name='open';
function toggle(showHideDiv, switchImgTag) {
        var ele = document.getElementById(showHideDiv);
        var imageEle = document.getElementById(switchImgTag);
        if(ele.style.display == "block") {
                ele.style.display = "none";
			createCookie(name,'false',0);
			imageEle.innerHTML = 'Show this window';
        }
        else {
                ele.style.display = "block";
                createCookie(name,'true',0);
			imageEle.innerHTML = 'Hide this window';
        }
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function createCookie(name,value,days) {
if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function checkCookie(name) {
    var x = readCookie(name)
    if (x == 'false') {
    	document.getElementById('showHideDiv').style.display = 'none'
	document.getElementById('switchImgTag').innerHTML = 'Show this window';
    } else if (x == 'true') {
        document.getElementById('showHideDiv').style.display = 'block';
	document.getElementById('switchImgTag').innerHTML = 'Hide this window';
    }
}
</script>
<style type="text/css">
body
{
width: 300px;
}
</style>
<body onload="javascript:checkCookie('open')">
<div id="showHideDiv" style="display:block;">Some bs to hide or not</div>
<div id="switchImgTag" style="float:right;border:black solid 1px;">Hide this window</div><br><br>
<button type="button" style="float:right;" onclick="toggle('showHideDiv', 'switchImgTag');">click here</button>
</body>

Link to comment
Share on other sites

thanks for this sunfighter. Only the problem that I had was that I have about 20 of those divs.

I came up with this solution. Let me know what you think.

<?php 
if (isset($_COOKIE['contentDivImg9'])) {
$showhide9 = $_COOKIE['contentDivImg9']; }
	else { $showhide9 = "block"; }
	if ( $showhide9 == "block") { $button9 = 'x'; } else { $button9 = 'o';}
?>

 

Just have to find something for the alt text: Show this window or Hide this window.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.