-1

From the current web page how can I get the urls or src of all the images in the same page using java script?

  • 1
    Possible duplicate (http://stackoverflow.com/questions/5809051/how-to-get-all-the-image-sources-on-a-particular-page-using-javascript) – Shubhojit Jul 22 '15 at 05:02
  • I tried this code snippet as part of a popup window, and i think that instead of the current window being the active tab, the current window is being taken as the popup window. So the number of images is coming as 0. Is there any way to rectify my mistake? – Kavitha Ramachandran Jul 22 '15 at 14:48

3 Answers3

0

Pretty easy,....

 <script>
 function extractImg(){
    var images = document.getElementsByTagName("img");
    var myImgs[];
    for (i=0; i < images.length; i++) 
    {
      myImgs.push(images[i].src);
    }
    return myImgs;
  }
 </script>
Kylie
  • 11,421
  • 11
  • 47
  • 78
0
var imgs = document.getElementsByTagName('img');

var result = [];

for (var i = 0, il = imgs.length; i < il; ++i) {
  if (imgs[i].src) result.push(imgs[i].src);
}

results now contains the src's of image tags found on the page.

Zach Dahl
  • 609
  • 5
  • 12
0

Assuming you are using jQuery, you could use something like this:

var Collection = $('img');
var ImgSources = [];
$.each(Collection,function(i,elm){
    ImgSources.push($(elm).attr('src'));
});

... now you have an array ImgSources, with all your image’s sources as strings.

Ole Sauffaus
  • 534
  • 3
  • 13