tags:

views:

40

answers:

3

I have a simple script to fade in the homepage of the website, something like:

jQuery(document).ready(function(){    
  $('body.home').hide();
  $('body.home').fadeIn('slow');
});

However, I would like the script to work only one time, once the website loads initially. On subsequent clicks when, say, a user goes to a secondary page and then returns to the homepage, I don't want the homepage to fade in again, it should be just homepage loading without fading.

Is there a way to do it with just jQuery code, maybe somehow execute the function only once and then stop it from executing? Any tips and code snippets will be greatly appreciated!

+6  A: 

You could set a cookie.

$(function () {
  if ($.cookie("loaded") != "true") {
      $('body.home').hide().fadeIn('slow');
      $.cookie("loaded", "true");
  }
});
Stephen
A: 

If you're loading "other parts" of your site only via AJAX calls, you can store a value in global javascript namespace which just indicates that you already executed that action.

Otherwise, you're doing a complete page refresh (redirect to another site) there is no clean javascript solution. In that case you need to store a cookie, make usage of the local storage or bring your server script into play.

jAndy
I see. I was just hoping that there might be a way to stop the function from repeat execution so there's no fading during just the current browser session, but I see what you say about a 'complete page refresh'. I guess setting cookie would be the only solution. Thanks all!
ninusik
+1  A: 

you could dive into the world of php and check to see if a session exists, if there in not one.. fade in. I could post some code if you wanted to go down this route

A quick read if your new to php and sessions: http://www.tizag.com/phpT/phpsessions.php

Adam