Languages
[Edit]
EN

JavaScript - preload image

3 points
Created by:
Paris-Bateman
504

In this article, we would like to show you how toĀ preload imagesĀ using JavaScript.

Quick solution:

<script>

    function preloadImage(url) {
        var image = new Image();
        image.src = url;
        image.loading = 'lazy';
        return image;
    }

    var url = 'https://dirask.com/static/bucket/1624644642609-mbQLqBEYJX--example-img-730w.jpg';
    var img = preloadImage(url);

</script>

Note: In this case new image object is created in the web browser memory causing image fetching.

or:

<script>

    function preloadImage(url) {
        var link = document.createElement('link');
        link.href = url;
        link.rel = 'preload';
        link.as = 'image';
        document.head.appendChild(link);
    }

    preloadImage('https://dirask.com/static/bucket/1624644642609-mbQLqBEYJX--example-img-730w.jpg');

</script>

Note: In this case new link element is created in head element causing image fetching.

Ā 

Practical examples

In the below examples, we create preloadImage() function that loads images for indicated URLs.

Example 1

In this example, we create all image elements before, and mount them inside the containerĀ element on click event.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button onclick="useImage(img1)">Image 1</button>
  <button onclick="useImage(img2)">Image 2</button>
  <button onclick="useImage(img3)">Image 3</button>
  <br /><br />
  <div class="images-container"></div>
  <script>

    function preloadImage(url) {
        var image = document.createElement('img');
        image.src = url;
        image.loading = 'lazy';
        return image;
    }


    // Usage example

    var imagesContainer = document.querySelector('.images-container');

    var url1 = 'https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png';
    var url2 = 'https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png';
    var url3 = 'https://dirask.com/static/bucket/1633375165831-yjQ7G6WQeL--image.png';

    // images preloading:

    window.img1 = preloadImage(url1);
    window.img2 = preloadImage(url2);
    window.img3 = preloadImage(url3);

    var currentImage = null;


    // Helper methods

    function useImage(image) {
        if (currentImage) {
            imagesContainer.removeChild(currentImage);
        }
        imagesContainer.appendChild(image);
        currentImage = image;
    }

  </script>
</body>
</html>

Example 2

In this example, we preload imagesĀ into the web browser cache.

The main idea is to:

  1. create image objects indicating URLs,
  2. the imagesĀ will be loaded andĀ by default stored insideĀ the cache,
  3. next calls to the sameĀ URLs will cause loading from the cache.
// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button onclick="image.src='/static/bucket/1631898942509-VMYrnXyYZv--image.png'">Image 1</button>
  <button onclick="image.src='/static/bucket/1574890428058-BZOQxN2D3p--image.png'">Image 2</button>
  <button onclick="image.src='/static/bucket/1633375165831-yjQ7G6WQeL--image.png'">Image 3</button>
  <br /><br />
  <img id="image" src="/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  <script>

    window.image = document.querySelector('#image');

    function preloadImage(url) {
        var image = new Image();
        image.src = url;
        image.loading = 'lazy';
        return image;
    }

    // images preloading (we can run it in window load event):

    preloadImage('/static/bucket/1631898942509-VMYrnXyYZv--image.png');
    preloadImage('/static/bucket/1574890428058-BZOQxN2D3p--image.png');
    preloadImage('/static/bucket/1633375165831-yjQ7G6WQeL--image.png');

  </script>
</body>
</html>

Example 3

In this example, we use built-in mechanism thatĀ tries to load indicated images as soon as it is possible.

The main idea is to put specialĀ link elements inside the head element that causeĀ images preloading.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button onclick="image.src='/static/bucket/1631898942509-VMYrnXyYZv--image.png'">Image 1</button>
  <button onclick="image.src='/static/bucket/1574890428058-BZOQxN2D3p--image.png'">Image 2</button>
  <button onclick="image.src='/static/bucket/1633375165831-yjQ7G6WQeL--image.png'">Image 3</button>
  <br /><br />
  <img id="image" src="/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  <script>

    window.image = document.querySelector('#image');

    function preloadImage(url) {
        var link = document.createElement('link');
        link.href = url;
        link.rel = 'preload';
        link.as = 'image';
        document.head.appendChild(link);
    }

    // images preloading (we can run it in window load event):

    preloadImage('/static/bucket/1631898942509-VMYrnXyYZv--image.png');
    preloadImage('/static/bucket/1574890428058-BZOQxN2D3p--image.png');
    preloadImage('/static/bucket/1633375165831-yjQ7G6WQeL--image.png');

  </script>
</body>
</html>

References

  1. Image() - Web APIs | MDN

Alternative titles

  1. JavaScript - image preloading
  2. JavaScript - preload HTML img element
  3. JavaScript - preload HTML image element
  4. JavaScript - load image to memory only
  5. JavaScript - load image to cache only
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join