5

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 !

psiyumm
  • 6,437
  • 3
  • 29
  • 50
keyloags
  • 77
  • 5

3 Answers3

3

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

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
  • I tried using the sessionStorage, it works for the animation not to be displayed if i come back on the index, but if I refresh, the animation won't display either – keyloags Nov 14 '15 at 12:00
  • @keyloags for this, i would suggest to use $(window).unload(); function in combination with the sessionStorage described above – Sebass van Boxel Nov 14 '15 at 12:02
  • Could you give me an example ? I don't really know how to manipulate the unload() function... – keyloags Nov 14 '15 at 12:03
  • I'm going to stick with the sessionStorage. It will refresh the animation only if the browser is closed, but I'll work with it ! Thanks :) – keyloags Nov 14 '15 at 12:11
1

Try this one !!

 if (!sessionStorage.isActive) {
        $('.your-div').animate(); //write your code to animate
        sessionStorage.isActive= 1;
    }
Jinu Kurian
  • 9,128
  • 3
  • 27
  • 35
0

Use of sessionStorage may be helpful, You can save

animationContent = document.getElementById("yourID").innerHTML;
sessionStorage.setItem('animationContent', animationContent);
if(sessionStorage.getItem('messageContent')){
    yor animation function;
}

Or

.one ensures this is done only once

$(document).once('ready', function() 
{
    Your Code here;
});