Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Show hide cookie amend

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

0
danwpeters
Asked:
danwpeters
  • 9
  • 5
  • 3
2 Solutions
 
Bane83Commented:

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

0
 
danwpetersAuthor 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.
0
 
Michel PlungjanIT ExpertCommented:
It would be smarter to save the name of the shown div or a list of shown divs and hide the rest.

Anyway

var listOfItems = ["featureIntro","filter"];
 
function init() {
  for (var i=0;i<listOfItems.length;i++) {
    var prev = readCookie(listOfItems[i]);
    document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'none'; 
  }
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
danwpetersAuthor Commented:
Thanks mplungjan, they open and close ok again now, but reset when the page is reloaded.
0
 
danwpetersAuthor Commented:
Tested it in firefox, and it works. But in IE it doesnt, forgets your div show/hide state when reloading the page.
0
 
Michel PlungjanIT ExpertCommented:
remove the window.onload=init and put this at the bottom of the page

<script>
init()
</script>
0
 
danwpetersAuthor Commented:
Hmm still the same, any ideas?
0
 
Bane83Commented:
Not sure if this is your problem, or if you've fixed it yet or not, but I believe there might be a small mistake with mplungjian's script:

var listOfItems = ["featureIntro","filter"];
 
function init() {
  for (var i=0;i<listOfItems.length;i++) {
    var prev = readCookie(listOfItems[i]);
    document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'none';
  }
}

Here, if the cookie is null it sets the display to 'none', which would start the divs as hidden.  Going by your original code, you may want to switch that to:

document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'';
0
 
danwpetersAuthor Commented:
bane, thanks for fixing it, I want to give you points too, how can I?
0
 
Bane83Commented:
There's a split functionality somewhere, I don't remember exactly where, but it's fine.  mplungjan did all of the real work.
0
 
Michel PlungjanIT ExpertCommented:
And our joint efforts was only worth a "B" ?
0
 
danwpetersAuthor 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.
0
 
Michel PlungjanIT ExpertCommented:
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.
0
 
danwpetersAuthor Commented:
Thanks for your help guys
0
 
danwpetersAuthor 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!
0
 
Michel PlungjanIT ExpertCommented:
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.
0
 
danwpetersAuthor 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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now