Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a jquery function only once in one page, per session?

I have a jQuery function, that produces an animation when my index.html is loaded. I would like this animation to happen only once. If you go on an other page, then come back on the index.html, the animation should not happen.

But I would also like that when the index page is refresh, the animation is displayed.

I don't know if you get it... Anyway thanks !

like image 779
keyloags Avatar asked Sep 27 '22 05:09

keyloags


2 Answers

Javascripts sessionStorage can make this possible.

This sessionStorage-variable will only be accessible while and by the window that created it is open. So, after closing the window, the sessionStorage variable will be undefined.

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

Simple wrap this in an if-statement to check whether to show the animation or not.

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

like image 57
Sebass van Boxel Avatar answered Nov 01 '22 16:11

Sebass van Boxel


Try this one !!

 if (!sessionStorage.isActive) {
        $('.your-div').animate(); //write your code to animate
        sessionStorage.isActive= 1;
    }
like image 23
Jinu Kurian Avatar answered Nov 01 '22 15:11

Jinu Kurian