//, Photoshop, WordPress/Designing and Coding a WordPress Theme From Scratch (Part 5)

Designing and Coding a WordPress Theme From Scratch (Part 5)

Image heavy websites have one problem: load time. So if you’re designing a WordPress theme that relies heavily on images like one made in Photoshop, you want to do everything you can to increase speed. a) You want the images to display as shortly after the text does as possible and b) you want to do everything to help users on slower connections.

One way to do this is with javascript, the other way is with CSS.

Preloading Images with Javascript

Javascript is unfortunately abused all over the web. It’s used for scripts that create pop-up ads, pop-under ads, and to change browser homepages. So the only draw back to this approach is that some users have deliberately turned off javascript in their browsers to thwart abusers. No big deal. If the user has javascript turned off, the technique I’m about to share will be ignored and the page will load normally.

  1. So how is it done? In the folder that contains your index.html file and stylesheets, create a folder called ‘Scripts’.
  2. Let’s create a blank document in the text editor and call it preload.js
  3. We’re going to create an array of calls for images that will start to load as soon as the head of the page loads. List all the images you want to have ‘headstart’ on loading here. This can include images that are linked to on the page but that don’t appear on the page. When dealing with a Photoshop design that is being marked up to XHTML you should place all the slices here.
  4. Next you want to place the following code in the header section of your HTML document just after the <title> and <meta> tags.
    <script type="text/javascript" src="scripts/preload.js"> </script>
    

    This tells the HTML document to look for the script before the stylesheet and subsequently, to request the array of images the stylesheet needs to load. Alternatively you could place the javascript inline but I prefer using external documents.

  5. The contents of the Javascript file should look like this:
    if (document.images)
    {
    preload_image = new Image();
    img_path = new Array();
    
       img1 = new Image();
       img2 = new Image();
       img3 = new Image();
       img4 = new Image();
       img5 = new Image();
       img6 = new Image();
       img7 = new Image();
       img8 = new Image();
       img9 = new Image();
       img10 = new Image();
       img11 = new Image();
       img12 = new Image();
       img1 = new Image();
       img1.src = "../images/yourimage_01.png";
       img2.src = "../images/yourimage_02.png";
       img3.src = "../images/yourimage_03.png";
       img4.src = "../images/yourimage_04.png";
       img5.src = "../images/yourimage_05.png";
       img6.src = "../images/yourimage_06.png";
       img7.src = "../images/yourimage_07.png";
       img8.src = "../images/yourimage_08.png";
       img9.src = "../images/yourimage_09.png";
       img10.src = "../images/yourimage_10.png";
       img11.src = "../images/yourimage_11.png";
       img12.src = "../images/yourimage_12.png";
       img13.src = "../image/yourimage_13.png";
    
    for(var i = 0; i<=img_path.length; i++)
    preload_image.src = img_path[i];
    }
    
  6. That’s it. You can use this to preload images for the next page, icons, or any number of things but the idea is to use it to call the array before they’re called elsewhere on the page.

Preloading Images with CSS

Another method is to preload images in the stylesheet. This method is fairly useless on the actual homepage but it can be used to preload all sorts of images on subsequent pages. Lets look at the code:

     .hiddenPic {display:none;}

This class should be placed somewhere in your stylesheet. You can place it inline in your html document and it would look like this:

     <style type="text/css">
     .hiddenPic {display:none;}
     </style>

Next, at the bottom of your html page just before the </body> closing tag, we need to call all the images from the next page we want preloaded and apply the .hiddenPic class:

     <img src="yourimage.jpg" alt="" title="" height="" width="" class="hiddenPic">

     <img src="yourimage01.jpg" alt="" title="" height="" width="" class="hiddenPic">

     <img src="yourimage02.jpg" alt="" title="" height="" width="" class="hiddenPic">

</body>

How does it work? Essentially you’re calling the images when the browser reads the page and hiding them before they are ever displayed with the display:none; line. The idea is that the browser downloads the images and caches them anyways so that when the user clicks through to the next page the users browser will note that the images being requested have already been cached and won’t waste time downloading them again.

Now that load time has been reduced, in Part 6 we’ll get back to marking up our XHTML to PHP and developing our WordPress theme.

2010-05-19T22:52:06+00:00 April 28th, 2008|JavaScript, Photoshop, WordPress|0 Comments

About the Author:

Leave A Comment