开发者

jQuery function on initial home page load only

开发者 https://www.devze.com 2023-02-13 04:52 出处:网络
I know that Apple had done this on their homepage for the Beatles.. well, at least I think they did. but, I have an animation happening to my header and navigat开发者_Go百科ion links on my webpage th

I know that Apple had done this on their homepage for the Beatles.. well, at least I think they did.

but, I have an animation happening to my header and navigat开发者_Go百科ion links on my webpage that I want to only animate on the load of the home page only. Now, I know I can specifically add the JS to the home page but is there also a way to limit the animation to only work when a visitor FIRST visits the site in one session?


You can record visits in a cookie, then interrogate that cookie


You'd probably want to use cookies for this. Here's an explanation of how they work: http://www.quirksmode.org/js/cookies.html.

Basically, you store a boolean value once the animation is first played, and then it won't be played again until the cookie expires.


You can set a cookie via JavaScript after the initial load, and then check for that cookie in subsequent requests. If it exists, don't play the animation. If you set the cookie to expire as soon as the browser is closed, the animation will replay the next time they visit the site.

If you're using jQuery, I suggest the jquery.cookie.js plugin for handling cookie interactions.


Sessions, created at visits and destroyed at exits.

Cookies... they should work, but there is the time problem; whatever time-needed-to-pass-to-show-the-animation-again you choose, some visitors will receive undesired results.


You could use code server side be it PHP, ASP etc. You can use a cookie or the session to store a boolean value to determine if it's the first time they've loaded the page and have your JS render your image at that time.

  • User loads page
  • If JS does not find cookie renders image and creates a cookie
  • If JS does find cookie image is not rendered
0

精彩评论

暂无评论...
验证码 换一张
取 消