Solved

Show hide cookie amend

Posted on 2008-10-06
17
213 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

0
Comment
Question by:danwpeters
  • 9
  • 5
  • 3
17 Comments
 
LVL 10

Expert Comment

by:Bane83
Comment Utility

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
 

Author Comment

by:danwpeters
Comment Utility
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 400 total points
Comment Utility
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
 

Author Comment

by:danwpeters
Comment Utility
Thanks mplungjan, they open and close ok again now, but reset when the page is reloaded.
0
 

Author Comment

by:danwpeters
Comment Utility
Tested it in firefox, and it works. But in IE it doesnt, forgets your div show/hide state when reloading the page.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
remove the window.onload=init and put this at the bottom of the page

<script>
init()
</script>
0
 

Author Comment

by:danwpeters
Comment Utility
Hmm still the same, any ideas?
0
 
LVL 10

Assisted Solution

by:Bane83
Bane83 earned 100 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:danwpeters
Comment Utility
bane, thanks for fixing it, I want to give you points too, how can I?
0
 
LVL 10

Expert Comment

by:Bane83
Comment Utility
There's a split functionality somewhere, I don't remember exactly where, but it's fine.  mplungjan did all of the real work.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
And our joint efforts was only worth a "B" ?
0
 

Author Comment

by:danwpeters
Comment Utility
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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
 

Author Closing Comment

by:danwpeters
Comment Utility
Thanks for your help guys
0
 

Author Comment

by:danwpeters
Comment Utility
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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
 

Author Comment

by:danwpeters
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now