<?xml version="1.0" encoding="utf-8"?>
<!-- If you are running a bot please visit this policy page outlining rules you must respect. http://www.livejournal.com/bots/ -->
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:lj="http://www.livejournal.com">
  <id>urn:lj:livejournal.com:atom1:cattlyst</id>
  <title>Unhandled Exception</title>
  <subtitle>The infuriating ramblings of a garden variety geek</subtitle>
  <author>
    <name>cattlyst</name>
  </author>
  <link rel="alternate" type="text/html" href="http://cattlyst.livejournal.com/"/>
  <link rel="self" type="text/xml" href="http://cattlyst.livejournal.com/data/atom"/>
  <updated>2007-08-05T11:25:09Z</updated>
  <lj:journal userid="13528013" username="cattlyst" type="personal"/>
  <link rel="service.feed" type="application/x.atom+xml" href="http://cattlyst.livejournal.com/data/atom" title="Unhandled Exception"/>
  <link rel="hub" href="http://pubsubhubbub.appspot.com/"/>
  <entry>
    <id>urn:lj:livejournal.com:atom1:cattlyst:862</id>
    <link rel="alternate" type="text/html" href="http://cattlyst.livejournal.com/862.html"/>
    <link rel="self" type="text/xml" href="http://cattlyst.livejournal.com/data/atom/?itemid=862"/>
    <title>Graceful Web Image Loading</title>
    <published>2007-08-05T10:31:26Z</published>
    <updated>2007-08-05T11:25:09Z</updated>
    <category term="web design"/>
    <category term="code"/>
    <content type="html">I've only had this Journal for a few hours, and I'm already going to post some cool coding techniques. Yes, you should be afraid.&lt;br /&gt;&lt;br /&gt;Images are an integral part of many websites - they help to simplify navigation and to enhance the themes of your website. But when images are the showcase of a web page, the images often have large dimensions or are animated, resulting in a large file size. These file sizes can slow even the most impressive broadband Internet down, particularly on free image hosters, leaving your visitors in the dark for seconds or minutes whilst the showcase of your page loads.&lt;br /&gt;&lt;br /&gt;Today I'm going to take you through an easy way to load images gracefully so your viewers can feel comfortable that something is happening whilst the image loads in the background. We're going to do this by using a placeholder animation which ticks over whilst we wait.&lt;br /&gt;&lt;br /&gt;&lt;a name="cutid1"&gt;&lt;/a&gt;&lt;div class="ljcut" text="Read more..."&gt;OK, let's start with a straightforward example of what we're trying to achieve.&lt;br /&gt;Load up the example webpage &lt;a href="http://www.activex.com.au/ny/test.html"&gt;here&lt;/a&gt;, where will be gracefully loading one of &lt;span class='ljuser ljuser-name_sezzlybottom' lj:user='sezzlybottom' style='white-space: nowrap;'&gt;&lt;a href='http://sezzlybottom.livejournal.com/profile'&gt;&lt;img src='http://l-stat.livejournal.com/img/userinfo.gif' alt='[info]' width='17' height='17' style='vertical-align: bottom; border: 0; padding-right: 1px;' /&gt;&lt;/a&gt;&lt;a href='http://sezzlybottom.livejournal.com/'&gt;&lt;b&gt;sezzlybottom&lt;/b&gt;&lt;/a&gt;&lt;/span&gt;'s fantastic graphics.&lt;br /&gt;&lt;br /&gt;If it loaded a tad too fast for you to see, visit the page again and press Ctrl-F5, which should reload the page from scratch.&lt;br /&gt;&lt;br /&gt;So, what's happening here? Let me break it down.&lt;br /&gt;&lt;br /&gt;All of the magic of this code is client side JavaScript and CSS, so viewing the source of the example page above is an excellent way to understand what I'm telling you.&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;We have created a css style &lt;span style="font-style: italic;"&gt;loadImage&lt;/span&gt; which we'll apply to each of our images, which places an appopriate border around the image and sets the background of the image to our loading &lt;a href="http://i18.tinypic.com/4mwl4p5.gif"&gt;animation&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;.loadImage {&lt;br /&gt;	border: 1px solid #000000;&lt;br /&gt;	background-image: url(images/load.gif);&lt;br /&gt;	background-position: center center;&lt;br /&gt;	background-color: #FFFFFF;&lt;br /&gt;	background-repeat: no-repeat;&lt;br /&gt;}&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;We've laid the image tag out (you could do more than one image easily) with the dimensions of our final image, but with the src of &lt;span style="font-style: italic;"&gt;waiting.gif&lt;/span&gt;. This image is simply a 1 pixel by 1 pixel transparent gif which fills the space whilst your final image loads. The use of &lt;span style="font-style: italic;"&gt;waiting.gif&lt;/span&gt; was necessary to successfully render the page in all browsers.&lt;br /&gt;&lt;br /&gt;Also notice that each of the images uses the loadImage style we defined before, and has it's own unique id, which is important for later.&lt;br /&gt;&lt;pre&gt;&amp;lt;img id="testimage" class="loadImage" width="800" height="600" &lt;br /&gt;src="images/waiting.gif" /&amp;gt;&lt;/pre&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;Now, for each of the images, we execute a line of JavaScript to get our final images loading in the background.&amp;nbsp; If you're loading multiple images, you can combine each of these lines into a single set of&lt;span style="font-style: italic;"&gt; &amp;lt;script&amp;gt;&lt;/span&gt; tags. &lt;br /&gt;&lt;br /&gt;In each line we call the &lt;span style="font-style: italic;"&gt;setImageLoad&lt;/span&gt; function (which we'll introduce in the next step). The first parameter is the url of the final image we want to load, the second parameter is the name of the unique id we set in the previous step for the associated image tag.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;pre&gt;setImageLoad('http://img340...flyaway1nv9.png', 'testimage');&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;Finally, we include our global javascript into the head of the page. As you can see, the setImageLoad function loads the final image in the background until the loading is complete. When the loading is complete, the src value of the image tag associated with our unique id is changed, and the background image is removed (in case you have a partially transparent final image).&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;script type="text/javascript"&amp;gt;&lt;br /&gt;&amp;lt;!--&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;function setImageLoad(url, img)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var myImage = new Image;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myImage.src = url;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myImage.onload = function()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var target = getItem(img);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(target)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; target.src = myImage.src;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; target.style.backgroundImage = '';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function getItem(id)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var itm = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(document.getElementById)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itm = document.getElementById(id);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if(document.all)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itm = document.all[id];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if(document.layers)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itm = document.layers[id];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return itm;&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;//--&amp;gt;&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Arial,Verdana,Sans-Serif;"&gt;We're all Done. Load up the page in your web browser and watch as our images gracefully load. Just a few bytes of loading and waiting images have to load whilst our much larger image is loaded in the background.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;That's my little tutorial for today. I hope someone can find it useful.&lt;br /&gt;Drop me a line if you need help getting it working.&lt;br /&gt;-Nick&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>urn:lj:livejournal.com:atom1:cattlyst:547</id>
    <link rel="alternate" type="text/html" href="http://cattlyst.livejournal.com/547.html"/>
    <link rel="self" type="text/xml" href="http://cattlyst.livejournal.com/data/atom/?itemid=547"/>
    <title>Premièrement</title>
    <published>2007-08-05T05:24:57Z</published>
    <updated>2007-08-05T05:30:35Z</updated>
    <category term="movies"/>
    <category term="welcome"/>
    <content type="html">Welcome to the first entry of my Journal/Blog! &lt;br /&gt;I rather suspect everything will go downhill from here, but what the hell.&lt;br /&gt;&lt;br /&gt;I wrote a short bio about myself on my User Info page, so if you're fortunate enough to know nothing about me, you're welcome to head over there and read up on the not particularly juicy details.&lt;br /&gt;&lt;br /&gt;This journal will be a place for me to jot down random thoughts, share my skills and experience in computing and (should it prove to be interesting enough) details about my life too. &lt;br /&gt;&lt;br /&gt;&lt;a name="cutid1"&gt;&lt;/a&gt;&lt;br /&gt;Some friends and I went and saw the movie Fracture yesterday, so I thought I'd do a little write up to get the ball rolling with this Journal. &lt;br /&gt;&lt;br /&gt;Fracture is the story of the manipulative, charming and slightly insane Ted Crawford (Anthony Hopkins), a wealthy engineer who plans and executes the perfect murder of his wife after discovering she's having an affair. The story follows Willy Beachum, a hotshot lawyer from the distric attourney's office, whose reputation is put on the line when he's tasked with prosecuting the cool and calculating Crawford.&lt;br /&gt;&lt;br /&gt;I thought the movie was pretty good, with a very engrossing and well developed plot line. It was, perhaps, a little bit weak towards the end, but otherwise I had no complaints. The characters were portrayed very well, particularly Hopkins as the insane genius.&lt;br /&gt;&lt;br /&gt;Definately worth a watch if you're up for a movie.&lt;br /&gt;&lt;br /&gt;Alright, that'll about do it for my first entry. With a bit of luck, I'll post more in the near future.&lt;br /&gt;&lt;br /&gt;Until next time,&lt;br /&gt;-Nick&lt;br /&gt;</content>
  </entry>
</feed>
