Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Craig Watson
27,930 PointsUsing the Browser Cache to keep an element hidden if close button has been clicked on a previous visit to site?
Hi everyone,
I have googled around and cant seem to get something that I need to know really.
I have a section of a sidebar that gives a quick overview of a few elements on the site, you can click the close button and away it goes.
But this is clearly going to be a bad experience if every time you visit the site you need to remove this section of the sidebar.
I am assuming I need to leverage something from the browser cache, but I have nevr had to use the cache before....
Can anyone help me with this little buggy nasty no one wants to see a notice more than once issue :)
Craig
1 Answer
Kevin Korte
28,149 PointsLots of answer on this, since you're just remembering the state of an element client side, I'd use localstorage. Basically, when the user clicks the close button, have some javascript that executes setting a localstorage value.
Than, when a page is loaded, check if the localstroage value exists, and is set to decide if your page should show the elements or not.
This will allow element state to persist. Lots of stack overflow questions that do something similar. Here is one on setting toggle set.
Craig Watson
27,930 PointsCraig Watson
27,930 PointsHi Kevin :)
Cant thank you enough for this answer! Real positive way to end what had been a frustrating day. I am shocked no blogs had popped up with this solution after a full morning spent googling things. But from my question I think I was probably assuming I needed to do something a lot more complicated.
Thanks again Kevin this was super helpful!