0

Right now the following code works, but the notification bar I have disappears if you refresh the page. It should only disappear if the user clicks the close button, but I am not sure how to do that.

jQuery(document).ready(function($) {
var showHowTo = localStorage.getItem("firstvisit");
if(!showHowTo) {
    // Stores visit
    localStorage.setItem("firstvisit", true);
    $('#how-to').show();
} else {
    $('#how-to').hide();
}
$("#close").click(function(){
    $("#how-to").hide();
});  
});
0

3 Answers 3

1

Set the flag in the close handler

jQuery(function ($) {
    $('#how-to').toggle(localStorage.getItem("closeHelp") != 'true');
    $("#close").click(function () {
        $("#how-to").hide();
        localStorage.setItem("closeHelp", 'true');
    });
});

Also could use .toggle() to simplify the code

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You are simply hiding it after the user visits the page first time.

You can slightly edit your code, to store the value in localStorage not on page load, but when user actually clicks the close button:

jQuery(document).ready(function($) {
   var showHowTo = localStorage.getItem("userClosedWindow");
   if (!showHowTo) {          
      $('#how-to').show();
   } 
   else {
      $('#how-to').hide();
   }
   $("#close").click(function(){
       // Stores close button click
       localStorage.setItem("userClosedWindow", true);
       $("#how-to").hide();
   });  
});

Comments

0

if(! showHowTo): if the user hasn't visited yet, show how-to (set showHowTo=true)

else (equivalent to if (showHowTo)): the user has visited; hide how-to. That is why it disappears if you refresh the page. On reload, showHowTo is still true (since you saved it as such in localStorage).

To fix this:

if (! localStorage.getItem("howToClosed")) {
    $("#how-to").show();
} // no else, because if how-to has been closed, it simply won't show up again
$("close").click(function() {
    $("#how-to").hide();
    localStorage.setItem("howToClosed", true);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.