Set a cookie on interaction

Hi,
i wanted to share a script to set a cookie, so an interaction would show only when cookie criteria is met, e.g: once a day/week etc… (you can change it as you see fit). i’m using js-cookie lib for that.

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) { 
  // get a reference to the loading wrapper
  const popUp = document.querySelector('CLASS-NAME-OF-THE-ELEMENT-U-ANIMATE');

  // get the 'seenAnimation' cookie and store in a seenAnimation variable
  const seenAnimation = Cookies.get('seenAnimation');
  
  // if the 'seenAnimation' cookie is undefined
  if(!seenAnimation){
    // set the 'seenAnimation' cookie 
    Cookies.set('seenAnimation', 1, { expires: 1 });
  }
  else{
    // else if the 'seenAnimation' cookie exists
    // the user has already seen the animation
    // and so
    // hide the loading-wrapper
    popUp.style.display = "none";
    popUp.style.opacity = "0";
}
});
</script>

in this script i’m hiding the loading wrapper if cookie exist by applying these styles:
popUp.style.display = “none”;
popUp.style.opacity = “0”;

3 Likes

Thanks very much for that - saved some messing about :slight_smile:

I used document.getElementById for getting the div to hide.

Thanks for sharing! Can be quite useful!

GOODNESS!! Best one yet.

I have tried a few and this one actually works like magic!!!

Fine, it works!
But how to call a Webflow animation then?
Or at least, setting a 100% opacity (1) with ease?
Thanks