How to call jquery function only once on one page per session?

I have a jQuery function that creates animations when loading my index.html . I would like this animation to happen only once. If you go to another page and then return to index.html , the animation should not happen.

But I would also like the animation to show when the index page is updated.

I don’t know if you will get it ... Thanks anyway!

+5
source share
2 answers

Javascripts sessionStorage can make this possible.

This sessionStorage variable will be available only when the created window is opened. So, after closing the window, the sessionStorage variable will be undefined.

You can simply create this using something like sessionStorage.firstVisit = 1 .

Just wrap this in an if-statement to check if the animation is showing or not.

For more information about this check http://www.w3.org/TR/webstorage/#the-sessionstorage-attribute

+3
source

Try it!

  if (!sessionStorage.isActive) { $('.your-div').animate(); //write your code to animate sessionStorage.isActive= 1; } 
0
source

Source: https://habr.com/ru/post/1235950/


All Articles