We help IT Professionals succeed at work.

Show hide cookie amend

danwpeters
danwpeters asked
on
237 Views
Last Modified: 2012-05-05
Hi,

I've got a cookie i'm using with a show/hide div, I need to configure it to work with more divs.

I've attached the full code, the bit I need to edit is...

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev;
  else
    document.getElementById('featureIntro').style.display='';
}

I tried the following to also use the cookie with the filter div im show/hiding.

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev || document.getElementById(filter).style.display = prev;
  else
    document.getElementById('featureIntro').style.display='' || document.getElementById(filter).style.display='';
}


Any ideas how I can do this?
window.onload=init;
/*function init()
{
	var prev = readCookie('featureIntro');
	if( prev !== null )
		document.getElementById('featureIntro').style.display = prev;
}*/
function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev;
  else
    document.getElementById('featureIntro').style.display='';
}
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 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 eraseCookie(name) {
	createCookie(name,"",-1);
}
function showhide(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		if (obj.style.display == "none"){
			obj.style.display = "";
		} else {
      		obj.style.display = "none";
		}
		if( null!==readCookie(id) )
		{
			eraseCookie( id );
		}
		createCookie(id,obj.style.display,365);
	}
}

Open in new window

Comment
Watch Question

Commented:

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
  {
    document.getElementById('featureIntro').style.display = prev; 
    document.getElementById('filter').style.display = prev;
  }
  else
  {
    document.getElementById('featureIntro').style.display='';
    document.getElementById('filter').style.display='';
  }
}

Open in new window

Author

Commented:
Hi, thanks but this has bugged it up. it remembers if I close them both, if I open filter it wont remember, if I open feature it will open both when i reload the page.
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks mplungjan, they open and close ok again now, but reset when the page is reloaded.

Author

Commented:
Tested it in firefox, and it works. But in IE it doesnt, forgets your div show/hide state when reloading the page.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
remove the window.onload=init and put this at the bottom of the page

<script>
init()
</script>

Author

Commented:
Hmm still the same, any ideas?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
bane, thanks for fixing it, I want to give you points too, how can I?

Commented:
There's a split functionality somewhere, I don't remember exactly where, but it's fine.  mplungjan did all of the real work.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
And our joint efforts was only worth a "B" ?

Author

Commented:
Hi mplungjan, I gave you a B as it wasnt complete, I was then going to give bane83 the same as he fixed the issue, but I couldnt assign any more points after accepting your answer. Sorry.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
I unaccepted for you to split points

I do not believe a "B" is in order when the fix was so obvious and I could have answered it myself had I not been asleep at the time (CET) -  so I did not get a chance to rectify the suggestion.
The actual joint solution is worth an "A" in my opinion.

Author

Commented:
Thanks for your help guys

Author

Commented:
i've marked it correctly now, thanks for unaccepting the points mplungian, it was the first time ive had to accept 2 answers in a question so wasnt sure how it worked correctly. I thought I would have to give a B to yours and an A to bane's as he completed it, if you had completed it I would have given you an A, however, at the end of the day it was bane who completed it, which is why I originally marked you as a B.

Your both A now, thanks again for your help!
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Thanks

My issue was really that I had presented you with some code and in 4 hours you closed it with a "B"
Had I been available when you discovered that my code assumed (not unreasonably) that you wanted to default the divs to collapsed, then I would have completed the question and you would have had no reason to give a "B". So basically I suggest you give the experts the time/chance to answer the question completely (with possible extra elaboration) instead of grading based on the first comment.

Author

Commented:
mplungian, I graded when my question was fully answered. The code wasn't working correctly in IE, you gave a response to this which did not work, Bane83 then stepped in and provided a fix, and so my question was answered fully. Which would suggest your answer was a B, as it had a bug, and Bane corrected it which made it an A. Your grades are fine now, so lets call this a day.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.