views:

804

answers:

5

Is there a way to make sure a (large, 300K) background picture is always displayed first BEFORE any other content is shown on the page?

On the server we have access to PHP.

+3  A: 

All the html content is served and parsed before it even starts to fetch the image, so you have a problem before you start.

You could circumvent this by programmatically hiding the content, and then triggering a "show" of it when the image is loaded.

ie:

<html>
  <body>
    <image here/>
    <div id="content" style="display:none;" >

    </div>
    <script type="psudocode"> 
     when(image.loaded){ 
          $("#content").show();
     }
    </script>
  </body>
</html>
Kent Fredric
The first sentence is inaccurate. A Browser may start downloading and even finish downloading other required resources even before the initial HTML resource has been fully received. Note also the question relates to a background image not an image element.
AnthonyWJones
A: 

I think the only way you'll be able to do this is with javascript - Send the user HTML that only contains your background image and some javascript that either waits for a certain amount of time before displaying the rest of the content or uses AJAX to retrieve the rest of the content (essentially the same thing).

Chris Shaffer
+3  A: 

If you have all content inside a container, you can probably come pretty close using this techique. It will also fail gracefully if javascript is disabled/unavailable.

So if you have to do this because of a manager or something, this is the method I would use.

<html><head><!-- head section --></head>
<body>
    <div id="container">
       <script type="text/javascript">
       <!--
         document.getElementById('container').style.display = 'none';
       -->
       </script>
       Content goes here
    </div>
    <script type="text/javascript">
    <!--
      document.getElementById('container').style.display = 'block';
    -->
    </script>
</body></html>

If you have very little content, however, it probably won't do much good. You could of course add a timer on the second javascript block, to delay it for a second or so :P

gnud
+2  A: 
<html><head>
<script type="text/javascript">
//Hide on load
onload=function(){
    document.body.style.display="none";
    var imag = new Image();

    imag.onload=function(){
     var main = document.body;
     main.style.backgroundImage="url("+this.src+")";
     main.style.display="";

    };
    imag.src="http://dayton.hq.nasa.gov/IMAGES/MEDIUM/GPN-2000-001935.jpg";
}
</script>
</head>
<body>
    YOU CAN' T SEE MEE!!
</body>
</html>
kentaromiura
+1  A: 

A possible way, if you don't want to rely on JavaScript, is to make a dummy page with only the background image. After a few seconds, it redirects to the real page, and the background will load quickly because it is already in cache.

Not a super attractive solution, if the timing is fixed, I reckon.
Note that 300KB is quite big for a background image. I have seen worse, somebody using a 1MB image: even with a relatively fast connexion, I could see the background load way after the elements of the page.

PhiLho