MrJustin
MrJustin

Reputation: 1076

jQuery Sessions, is there such a thing?

Is there possibly a way to store jQuery sessions without any plugins? If not, what plugins seem to work best in 'your' opinion. Is it possible to get a good solution for my problem without even using a session? I'm still an amateur to jQuery, but know my way around it.

My task @ hand is a 'members bar' which gets toggled via a button, and always sticks to the top of the page. Now if they login, it automatically closes the bar, and I want to get around it. So a session seems like the way to go, considering I have it set to display: none; by default.

I have been looking through the web and done some research on it, but have come up with nothing useful for my case.

Here is my code, it is VERY simple and works without flaw at the moment. This is done with WordPress and is on document ready.

    $('#member-focus').click(function() {
            $( "#bar-content" ).slideToggle("slow");
    });
            // Would like to check if toggle is supposed to be open
            // If it is, set #bar-content to display: visible;

Upvotes: 0

Views: 106

Answers (1)

Amine Hajyoussef
Amine Hajyoussef

Reputation: 4420

you can take advantage of the html5 sessionStorage, data will be stored as long the browser window or tab is open (or session is restored), an example of usage would be:

// saving an item
sessionStorage.setItem("bar", "top");
// querying content
sessionStorage.getItem("bar")

note that sessionStorage is binded to a single window (or tab) , if you want to retain data across different windows, consider using Cookies or localStorage(same API as sessionStorage).

if you want to support old browsers (non HTML5), you can simulate the Storage API using Cookies(here's a sample)

Upvotes: 2

Related Questions