Languages
[Edit]
EN

JavaScript - get src of all images on web page

0 points
Created by:
user4838
698

In this article, we would like to show you how to get src of all images on web page using JavaScript.

Quick solution:

const findImages = () => {
    const urls = [];
    for (const image of document.images) {
        urls.push(image.src);
    }
    return urls;
};

or:

const findImages = () => {
    const images = document.querySelectorAll('img');
    const urls = [];
    for (const image of images) {
        urls.push(image.src);
    }
    return urls;
}

 

Practical examples

1. Using document.images property

In this example, we use document.images property to get a collection of the images in the current HTML document. Then we iterate the collection using for...of loop to get src of all images.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <!-- Image 1: dirask logo -->
  <img src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  <script>

    var images = document.images;

    function findImages() {
        var urls = [];
        for (var i = 0; i < images.length; ++i) {
            urls.push(images[i].src);
        }
        return urls;
    }


    // Usage example:

    window.addEventListener('load', function() {
        console.log(findImages());
    });

  </script>
  <!-- Image 2: JavaScript logo -->
  <img src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
</body>
</html>

2. Using querySelectorAll() method

In this example, we use querySelectorAll() method to get the collection of all images on the web page. Then using for loop we create array of src properties of all images. 

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <!-- Image 1: dirask logo -->
  <img src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" />
  <script>

    function findImages() {
        var images = document.querySelectorAll('img');
        var urls = [];
        for (var i = 0; i < images.length; ++i) {
            urls.push(images[i].src);
        }
        return urls;
    }
    
    
    // Usage example:
    
    window.addEventListener('load', function() {
        console.log(findImages());
    });

  </script>
  <!-- Image 2: JavaScript logo -->
  <img src="https://dirask.com/static/bucket/1574890428058-BZOQxN2D3p--image.png" />
</body>
</html>

Note:

The findImages() function needs to be executed after window load event or at the end of the script to make sure that all items are loaded before the function execution.

 

See also

  1. JavaScript - find all images on web page

  2. JavaScript - find all HTML comments

References

  1. Document.images - Web APIs | MDN
  2. Document.querySelectorAll() - Web APIs | MDN

Alternative titles

  1. JavaScript - how to get all images on particular web page?
  2. JavaScript - get all images on web page
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