PDA

View Full Version : Retain function action all through website



Sleek Geek
12-17-2014, 02:50 AM
Trying to retain the action of the function below in all the pages.
Example: If the user closes the alert box, let it "alert box" stay closed on
all pages except if the user closes the browser and comes back. I tried local storage but that didn't work out well. How can this be
achieved?

My code is as below


$(document).ready(function() {
if($(".happening").length > 1 ) {
$(".happening-switch").click(function () {
$(".happening:visible").hide().next().show();

});
}});


$(document).ready(function() {
if($(".happening").length < 2 ) {
$(".happening-switch").click(function () {
$(".happening:visible").slideUp();
});



}});

jscheuer1
12-17-2014, 03:21 AM
There's no perfect way to do this without a login and database for each user. Failing that (which I admit is probably overkill in this sort of situation), reasonable success can be achieved using a session cookie set and read via javascript.

I see nothing in the code in your post that references local storage. It would probably be easier to adapt to a javascript session cookie if I could see that, or at least see the rest of the page. Since you're already using jQuery though, there is a good jQuery cookie unit I feel good about recommending:

http://plugins.jquery.com/cookie/

Set it with no expiration and it will expire at the end of the browser session. The only drawback with a cookie of this nature is that without javascript, or if the user has cookies turned off, or deletes them before you intended them to expire - all things that usually never happen - but if they do (and they will happen sometimes), the user experience will be a little different then you intend if the cookie is set and kept as programmed. Generally this is no big deal in the rare cases when it does occur. If you want more help, please post a link the page on your site that shows the problematic code.