开发者

How can I use Javascript to get a list of all picture URLs available on a site?

I'm curious as to how DownThemAll does this. Do they use Javascr开发者_开发百科ipt?

How can I get a list of all of the urls in a website using Javascript?


Using collections

Links: document.links (href)

Images: document.images (src)

Using DOM

document.getElementsByTagName('img')

Bookmarklet:

Live Demo

(function(){
  var imgs = document.getElementsByTagName('img'),t=[];
  for (var i=0, n=imgs.length;i<n;i++) 
    t.push('<a href="'+imgs[i].src+'"><img src="'+
                       imgs[i].src+'" width="100"></a>'); 
  if (t.length) {
    var w=window.open('','_blank'); 
    if (w) {w.document.write(t.join('&nbsp;'));w.document.close();}
    else alert('cannot pop a window');  
  }    
 })();

Now you can save the new page to your harddisk and there will be a dir full of images


var list=[];
var a=document.getElementsByTagName('img');
for (var i=0,l=a.length;i<l;i++)
{
    if (/\.(jpg|gif|png|jpeg)$/im.test(a[i].getAttribute('src')))
    {
        list.push(a[i].getAttribute('src'));
    }
}

This code would generate list of picture URL's which are used in <img>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜