Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display A Popup Only Once Per User

There have already been answers to this question but I am still unsure exactly how it works.

I am using the following HTML in my footer.php:

<div id="popup">     <div>         <div id="popup-close">X</div>             <h2>Content Goes Here</h2>     </div> </div> 

and the following Javascript:

$j(document).ready(function() {     $j("#popup").delay(2000).fadeIn();     $j('#popup-close').click(function(e) // You are clicking the close button     {     $j('#popup').fadeOut(); // Now the pop up is hiden.     });     $j('#popup').click(function(e)      {     $j('#popup').fadeOut();      }); }); 

Everything works great, but I want to only show the pop up once per user (maybe using the cookie thing all the forum posts go on about) but I do not know exactly how to incorporate it into the JS above.

I know that I will have to load the cookie JS in my footer with this:

<script type="text/javascript" src="scripts/jquery.cookies.2.2.0.min.js"></script>  

But that is all I understand, can anyone tell me exactly how the JS/jQuery should look with the cookie stuff added?

Thanks

James

like image 209
James Waterhouse Avatar asked Jun 12 '14 16:06

James Waterhouse


People also ask

How do I show a popup within a div?

Create a div with semi transparent background & show it on top of your content page on click. 2. Show your pop up div or alert div on top of the semi transparent dimming/hiding div.


2 Answers

*Note : This will show popup once per browser as the data is stored in browser memory.

Try HTML localStorage.

Methods :

  • localStorage.getItem('key');
  • localStorage.setItem('key','value');

$j(document).ready(function() {     if(localStorage.getItem('popState') != 'shown'){         $j('#popup').delay(2000).fadeIn();         localStorage.setItem('popState','shown')     }      $j('#popup-close, #popup').click(function() // You are clicking the close button     {         $j('#popup').fadeOut(); // Now the pop up is hidden.     }); }); 

Working Demo

like image 113
Shaunak D Avatar answered Sep 24 '22 18:09

Shaunak D


This example uses jquery-cookie

Check if the cookie exists and has not expired - if either of those fails, then show the popup and set the cookie (Semi pseudo code):

if($.cookie('popup') != 'seen'){     $.cookie('popup', 'seen', { expires: 365, path: '/' }); // Set it to last a year, for example.     $j("#popup").delay(2000).fadeIn();     $j('#popup-close').click(function(e) // You are clicking the close button         {         $j('#popup').fadeOut(); // Now the pop up is hiden.     });     $j('#popup').click(function(e)          {         $j('#popup').fadeOut();      }); }; 
like image 29
Calvin deClaisse-Walford Avatar answered Sep 21 '22 18:09

Calvin deClaisse-Walford