Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
geoffrey
28,736 PointsJquery play once a method, no more.
Hello there, I'm using Jquery right now to build a friend's portfolio.
What I want to achieve, is to slidedown() the header with the menu and nav when we arrive on the .index.html
Till there, there is no problem at all, I can do this easily, but however I would like the slidedown() animation to play ONLY once, the first time we load the page !
This means for exemple, If I arrive on the index.html, the animation plays and the menu appears, then I go to the about section for exemple, and if ever I go back to the index.html, I don't want the slideDown() animation to play again...
Obviously, If I close the website and then I load it again, the animation needs to be played.
The main issue I'm having right now, is the fact that everytime I go back on the index.html, the script is loaded again and the animation is played again.
So Is there a way to memorize the fact a method has been already used and we don't want it to be played again. Knowing that everytime I go back on the index.html, the JS script is loaded...
I hope I was clear, I'll be glad to have a solution, I've found nothing googling.
2 Answers
Matthew Bretag
4,978 PointsSetting a session cookie sounds like what you want here. There's plenty of options but if you're after a simple jQuery one you can try https://github.com/carhartl/jquery-cookie.
It has examples on that page and if you don't supply an 'expires' value it will just default to the session. Meaning if a user comes back to your site later it will be cleared. eg Create session cookie:
$.cookie('ran-slide-down', 'yes');
And you can check the value before deciding whether to slide by reading the cookie:
eg
if($.cookie('ran-slide-down') != 'yes'){
//cookie not set so do slide
//add slide code in here...
//after slider runs once set the cookie so it won't run again
$.cookie('ran-slide-down', 'yes');
}
geoffrey
28,736 PointsApproved, works like a charm what I do, as It's simple. Thank you.