Search code examples
javascriptjquerycookiessweetalertsweetalert2

One-Time Pop-Up Box (SweetAlert2)


I have been making use of a new pop-up box system called SweetAlert2, which makes use of Java & CSS. It looks very appealing to the user, and I wanted to use it on my website for a purpose which I am struggling to achieve. I wanted to create a one-time popup box and once it's closed the user will not see it again. I know many websites use cookies however I have no idea how to go about implementing this into my script. I would love some support with this!

<script>
  swal({
    title: 'Website Maintenance Complete!',
    text: 'For full update log please click <a href="">here</a>',
    type: 'success',
    confirmButtonText: 'Nice!'
  }) 
</script>

Above is the script I use to make the pop-up box appear when the page loads, and I only want it to appear once. If anyone can point me in the right direction or provide a solution that would be great. Cheers!


Solution

  • You could use cookies to detect returning visitors:

    if (-1 === document.cookie.indexOf('returning=true')) {
    
      // run only if cookie not found (-1 means not found)
    
      swal( ... ); // alert
      document.cookie = 'returning=true'; // set cookie
    }