Ayush
Ayush

Reputation: 42450

Keep track of which tab was being viewed when page is refreshed

I have a webpage which has three tabs, and depending on which tab is clicked, the appropriate content is visible. This showing/hiding of the content on clicking of tabs is handled by Javascript.

My issue is, if I am viewing the third tab and then refresh the page, the page does not refresh to the third tab's contents being visible but instead, back to the first tab's contents.

Is there any way, when the javascript executes, for me to add a GET parameter to the URL, so on page load my PHP script can check for the GET parameter and display the correct content?

Upvotes: 2

Views: 305

Answers (3)

Igor Nikolaev
Igor Nikolaev

Reputation: 4717

Addtab id to the URL as an anchor when tab is clicked, e.g. http://www.mysite.com/page.html#tabId

Then when page loads you check for presence of tab id in url and activate required tab

Upvotes: 2

deviousdodo
deviousdodo

Reputation: 9172

You have a few options:

  • use a fragment identifier (add something like #tab3 to the URL)
  • store in a cookie
  • use local storage

Depending on what browsers you want to support and how long you want to persist the option you can pick any of them.

Upvotes: 3

rsboarder
rsboarder

Reputation: 4782

You can use cookie, or link hash. For example: http://jqueryfordesigners.com/api-filter-find/

Upvotes: 0

Related Questions