0

I'm using HTML5 local storage to store a value, "clicked" and only display a pop-up if that value is set to "false", clicking ok on the pop-up changes the value to "true" and it then the pop ups are no longer triggered. I've been testing this and the code looks good, no errors but it still shows the popup on every click.

jQuery( document ).ready(function() {

   localStorage.setItem('clicked',"false");
   var clicked = localStorage.getItem('clicked');
   if(clicked = "false") {

     jQuery('.external').click( function(e) {

       var r = confirm("You are now leaving");
       if (r == true) {
         localStorage.setItem('clicked',"true");
         window.open(jQuery(this).prop('href'),"_blank");

       } else {

       }

       e.preventDefault();

    });
  }
  else {
  }
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sam
  • 5,150
  • 4
  • 30
  • 51
  • 2
    you're missing an equal sign, it should be `if (clicked == "false")`. Also, what will `confirm("You are now leaving")` return? As long as there is a value there it will be equal to `true`. – Gene Parcellano Jun 26 '15 at 20:22

1 Answers1

0
jQuery( document ).ready(function() {

  localStorage.setItem('clicked',"false");
  var clicked = localStorage.getItem('clicked');

    jQuery('.external').click( function(e) {
      e.preventDefault();
      var clicked = localStorage.getItem('clicked')


      if (clicked === "true") {
          return;
      }

      var r = confirm("You are now leaving");
      if (r == true) {
        localStorage.setItem('clicked',"true");
        window.open(jQuery(this).prop('href'),"_blank");
      } else {

      }

    });

});
Jonathan
  • 1
  • 3
  • Even so, it will be set to `false` on page load. The handler will then be set, always. The handler is never *unset*, and nothing *in* the handler checks the stored value. – Paul Roub Jun 26 '15 at 20:25
  • @jonathan it is still showing popup regardless of the localStorage value (I can verify the value changes in devtools) – Sam Jun 26 '15 at 20:34
  • Updated the code, now the if check is inside click event, we check if its been set to click, if so we stop the function otherwise we have the popup show – Jonathan Jun 26 '15 at 20:40