Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had prepared 3 images with different sizes (to have them being loaded asynchronously).</p> <p>They are ~18 Kb, ~70 Kb and ~200 Kb. You can use them (provided at the end of the topic).</p> <p><strong>UPDATED CODE:</strong></p> <p>Let's name this code <code>index.html</code> and save it to file:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt;&lt;head&gt; &lt;script type="text/javascript"&gt; window.addEventListener('load', function () { // Define images var photos = [ {url:'./img1.jpg', loaded:false}, {url:'./img2.jpg', loaded:false}, {url:'./img3.jpg', loaded:false}, {url:'./img1.jpg', loaded:false}, {url:'./img2.jpg', loaded:false}, {url:'./img3.jpg', loaded:false}, {url:'./img2.jpg', loaded:false} ], photos_DOM_Elements = [], photosLoaded = 0;// Loaded images counter container = document.getElementById('div');// Images container /** * Each image should have event listener for `onLoad` event... */ photos.forEach(function (photo, photoInd) { var currentImage = document.createElement('img'); currentImage.src = photo.url; photos_DOM_Elements.push(currentImage); // ...and this event should check whether it was the last image that haven't been loaded currentImage.addEventListener('load', function (evt) { photosLoaded++; if (photos.length === photosLoaded) { alert('Loaded'); // Move images to a visible container photos_DOM_Elements.forEach(function (DOM_Elem) { container.appendChild(DOM_Elem); }); } }); }); }); &lt;/script&gt; &lt;/head&gt;&lt;body&gt; &lt;div id="div"&gt;&lt;/div&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>Save as <code>img1.jpg</code>: <a href="http://i.stack.imgur.com/UCKzG.jpg" rel="nofollow">http://i.stack.imgur.com/UCKzG.jpg</a><br> Save as <code>img2.jpg</code>: <a href="http://i.stack.imgur.com/bnU3c.jpg" rel="nofollow">http://i.stack.imgur.com/bnU3c.jpg</a><br> Save as <code>img3.jpg</code>: <a href="http://i.stack.imgur.com/TUaYr.jpg" rel="nofollow">http://i.stack.imgur.com/TUaYr.jpg</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload