16

I have a site with a rotating header image (you've all seen them). I want to do the following:

  1. Load the entire page plus the first header image
  2. Start the header image slideshow transitioning every x seconds or when the next image has finished loading, whichever is later

I haven't really need an example that truly does this.

Alex Angas
  • 59,219
  • 41
  • 137
  • 210
DevDevDev
  • 5,107
  • 7
  • 55
  • 87

6 Answers6

24

you can try

$(function()
{

$(window).bind('load', function()
{

// INSERT YOUR CODE THAT WILL BE EXECUTED AFTER THE PAGE COMPLETELY LOADED...

});
});

i had the same problem and this code worked for me. how it works for you too!

Website-Chef
  • 241
  • 2
  • 2
15

Well the first can be achieved with the document.ready function in jquery

$(document).ready(function(){...});

The changing image can be achieved with any number of plugins

If you wish you can check if images are loaded with the complete property. I know that at least the malsup jquery cycle slideshow makes use of this function internally.

stimms
  • 42,945
  • 30
  • 96
  • 149
13

You probably already know about $(document).ready(...). What you need is a preloading mechanism; something that fetches data (text or images or whatever) before showing it off. This can make a site feel much more professional.

Take a look at jQuery.Preload (there are others). jQuery.Preload has several ways of triggering preloading, and also provides callback functionality (when the image is preloaded, then show it). I have used it heavily, and it works great.

Here's how easy it is to get started with jQuery.Preload:

$(function() {
  // First get the preload fetches under way
  $.preload(["images/button-background.png", "images/button-highlight.png"]);
  // Then do anything else that you would normally do here
  doSomeStuff();
});
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
7

The $(document).ready mechanism is meant to fire after the DOM has been loaded successfully but makes no guarantees as to the state of the images referenced by the page.

When in doubt, fall back on the good ol' window.onload event:

window.onload = function()
{
  //your code here
};

Now, this is obviously slower than the jQuery approach. However, you can compromise somewhere in between:

$(document).ready
(
  function()
  {
    var img = document.getElementById("myImage");

    var intervalId = setInterval(
                        function()
                        {
                          if(img.complete)
                          {
                            clearInterval(intervalId);
                            //now we can start rotating the header
                          }
                        },
                        50);
  }
);

To explain a bit:

  1. we grab the DOM element of the image whose image we want completely loaded

  2. we then set an interval to fire every 50 milliseconds.

  3. if, during one of these intervals, the complete attribute of this image is set to true, the interval is cleared and the rotate operation is safe to start.

David Andres
  • 31,351
  • 7
  • 46
  • 36
  • 1
    window.onload = function() is a bad idea, it prevents you from setting onload from anything else or will erase any other "onload". – Bite code Oct 07 '09 at 09:25
  • 3
    @e-satis: To avoid overwriting the hooks that are already attached to the `window.onload`, attach your function with jQuery: `$(window).load(function () {});` – Andreas Grech Jan 05 '10 at 15:06
  • @e-satis: I mentioned window.onload specifically because the event handler code assigned to it is guaranteed to execute after images have loaded, which is different from the behavior of the jQuery ready function and somewhere at the other extreme of what the OP is looking for. You're right, there are better ways to attach events, but I didn't want to lose sight of the key points I was trying to convey. – David Andres Jan 15 '10 at 05:16
  • @Andreas: Good to know about the Load function. – David Andres Jan 15 '10 at 05:18
3

did you try this ?

$("#yourdiv").load(url, function(){ 

         your functions goes here !!!

}); 
1

If you pass jQuery a function, it will not run until the page has loaded:

<script type="text/javascript">
$(function() {
    //your header rotation code goes here
});
</script>
Ryan Doherty
  • 38,580
  • 4
  • 56
  • 63