1

I'm using Javascript to hide the site map on each page of a site (so that it will be visible on browsers with Javascript disabled). I then use a JQuery toggle to allow the user to reveal the Site Map (using a "Site Map" link in the footer of each page).

The Site Map "re-hides" each time the user navigates to another page, but I'd like to maintain the visibility of the Site Map across pages.

In other words: the Site Map should start out as hidden, but if the user toggles the visibility of the Site Map, it should stay visible while the user navigates from page to page until the user hides it again.

Mark G
  • 13
  • 2

2 Answers2

0

Set a cookie.

Read How do I set/unset cookie with jQuery? discussing how to do just that. discussing how to do just that.

Cookies allow you to track user preferences client side, so you dont have to rely on server based code.

Community
  • 1
  • 1
Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
0

The best solution now is to use localStorage :

// read
var hidden = localStorage['hidden']=='yes'; // defaults to false at first visit

// write
localStorage['hidden']= hidden ? 'yes' : 'no';

Those values are stored and available for all pages of your site (more precisely the origin) and are less prone to "cleaning" (external tools or in browser) than cookies.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Before choosing to use a html5 method, be sure to check the compatibility chart ( see http://www.html5rocks.com/en/features/storage ) and determine if you need support for non-supporting browsers (example: ie7). If you need support for older browsers, this won't work. – Damien Overeem May 27 '13 at 17:10
  • ie7 is no longer a supportable browser. Almost no modern page is correct on ie7 and nobody cares. – Denys Séguret May 27 '13 at 17:11
  • Thats why I didnt -vote. But as an example: I developed web based software for a hospital that didn't upgrade to ie8, because it breaks existing web based software. It'll probably be atleast another year before they update. It's not an ideal work in web development land.. – Damien Overeem May 27 '13 at 17:12
  • @DamienOvereem I wasn't saying your comment is bad. There's no problem in pointing limits. But for all normal cases (that is not your hospital and a few companies) I think cookies should be forgotten as an awkward thing from the past. – Denys Séguret May 27 '13 at 17:15
  • That will take a while. Server-side can't read localstorage, so we'll still be stuck with cookies for ie. session keys for a long long time. – Damien Overeem May 27 '13 at 17:17
  • I don't think so. Many web apps are ajax based now, they manage what is sent to the server in javascript. – Denys Séguret May 27 '13 at 17:19
  • Thanks, I've never used cookies, not wanting to leave crumbs behind. New to JS, I was really asking about how to use some sort of global variable, as in: one that initiates on first visit to the site, persists while navigating, then goes away as user leaves. Perhaps that's what "session" means, I just didn't even know what to search for! But now I'll search for session and localStorage. – Mark G May 27 '13 at 21:20
  • I'll keep an eye on browser compatibility, of course, but as this is a minor user convenience feature, I'm not too concerned about stubborn IE7 users. They're on their own as far as I'm concerned, and curses on MS and all those maintaining the existence of IE!! – Mark G May 27 '13 at 21:21
  • @MarkG Sessions are server based and initiated ie by the `session_start()` when using php (now you have something to search for). You have no way to remove data as soon as a user leaves, aside from storing data for a limited time and renewing that time on every request. Ie. if you set it for 1 minute and reset 1 minute after each page load, the data will be gone if a user does not reload a page within that minute. There are no solid ways to remove data "when a user leaves the site" because the user leaves after each request, but just comes back shortly after. Thats how web works. – Damien Overeem May 28 '13 at 17:55