View Full Version : Javascript Session Cookie to remember last tab

10-07-2011, 12:15 AM

I have a website where the user can navigate different categories by tab. The problem is, when they're on a tab other than the default one and they refresh the page or click to another page, it puts them back on the first tab. I know this can be solved by using a session cookie of some type, I just need some help on implementing it into my site.

http://www.thatswhyimbroke.com/ - so you can see what i'm talking about. If you look at the different tabs: Price High, price low, food & drink, etc. I want the user to be able to go to one of those, click to page 2 or refresh, and still be on that tab.

Any help would be much appreciated!

10-07-2011, 01:18 AM
You can do this by setting and reading a cookie and you can read about how to do that here (and copy the necessary functions):


However - in my opinion - it would be better to do it by listening to hash changes:
1) Let a click on a tab trigger a hash change
2) Listen to the hash change event and change content accordingly.

This would let the user navigate the site with the back and forward buttons in his browser, thus changing between tabs.

Hash changes are a bit tricky to listen to in a cross-browser-compliant way. But there are some nice libraries out there that does this trick. I haven't used any standalone ones in a while, but I have experience from several different JQuery plugins. The one I would recommend is:


10-07-2011, 01:26 AM
I get the theory behind it using this link: http://www.quirksmode.org/js/cookies.html

But I don't know how to implement it into those tabs... I'm not very good with javascript at all so implementing this into HTML/PHP is foreign to me.