Change See the bottom of this answer. I had a much better idea.
Original answer
Yes, itβs possible. Others have noted plugins for this, which are great in that they undergo preliminary testing and such, but if you want to do it yourself, it is surprisingly easy. You add img elements to the DOM (document object model):
function addAnImage(targetId, src, width, height) { var img; img = document.createElement('img'); img.src = src; img.style.width = width + "px"; img.style.height = height + "px"; target = document.getElementById(targetId); target.appendChild(img); }
(I could not immediately remember how to set the alt attribute, perhaps it will be img.alt = "..."; )
Naturally, you want to add some error to it. :-) So, for one of your images you want to add them to div pics , for example:
addAnImage('pics', 'images/cs12.png', 200, 200);
Configure the function to add images and call them when the page loads (using window.onload or any other that supports your JavaScript library, if any, for something a little earlier). For example, your script loading might look like this (I usually do not use window.onload , but this is convenient for example):
function pageLoad() { var images = [ {src: "images/cs9.png", width: 270, height: 270, alt: "teaching"}, {src: "images/cs1.png", width: 200, height: 200, alt: "teaching"}, {src: "images/cs3.png", width: 200, height: 200, alt: "teaching"},
When loading a window that will load the first image, and then schedule the next one to be loaded 200 ms (fifth of a second) later. When this happens, he will plan the next, etc. Etc. Until it loads all the images.
JavaScript libraries such as jQuery, Prototype, Closure, etc. usually have various helper functions for this kind of thing.
Updated Answer
This is good, but it means that you need to completely change the way your pages are arranged, and you need to mix content materials (image sources and sizes) with your JavaScript, etc. Blech.
How to do it: make all your image tags the same for the same image:
<div id="pics"> <img src="images/w270h270.png" width="270" height="270" alt="teaching"/> <img src="images/w200h200.png" width="200" height="200" alt="teaching"/> <img src="images/w200h200.png" width="200" height="200" alt="teaching"/> ...
These would be placeholders. Then add data attributes with a real image source to them:
<div id="pics"> <img src="images/w270h270.png" data-src="cs9.png" width="270" height="270" alt="teaching"/> <img src="images/w200h200.png" data-src="cs1.png" width="200" height="200" alt="teaching"/> <img src="images/w200h200.png" data-src="cs3.png" width="200" height="200" alt="teaching"/> ...
Attributes in the form of data-xyz will be valid with HTML5 (and they work today, they simply are not checked).
Now the magic: as soon as the main download is complete, you will go through the img tags in the DOM by updating their src to create your data-src attribute:
function pageLoad() { var nodeList, index;
Again, you will want to add error handling and not fully test, but in principle this should work.