List of All Background Images in DOM

前端 未结 7 1083
旧巷少年郎
旧巷少年郎 2020-12-29 12:00

What\'s the best way to find all of the background images on a given page using javascript?

The ideal end result would be an array of all of the url\'s.

相关标签:
7条回答
  • 2020-12-29 12:26

    //alert(getallBgimages())

    function getallBgimages(){
     var url, B= [], A= document.getElementsByTagName('*');
     A= B.slice.call(A, 0, A.length);
     while(A.length){
      url= document.deepCss(A.shift(),'background-image');
      if(url) url=/url\(['"]?([^")]+)/.exec(url) || [];
      url= url[1];
      if(url && B.indexOf(url)== -1) B[B.length]= url;
     }
     return B;
    }
    
    document.deepCss= function(who, css){
     if(!who || !who.style) return '';
     var sty= css.replace(/\-([a-z])/g, function(a, b){
      return b.toUpperCase();
     });
     if(who.currentStyle){
      return who.style[sty] || who.currentStyle[sty] || '';
     }
     var dv= document.defaultView || window;
     return who.style[sty] || 
     dv.getComputedStyle(who,"").getPropertyValue(css) || '';
    }
    
    Array.prototype.indexOf= Array.prototype.indexOf || 
     function(what, index){
     index= index || 0;
     var L= this.length;
     while(index< L){
      if(this[index]=== what) return index;
      ++index;
     }
     return -1;
    }
    
    0 讨论(0)
  • 2020-12-29 12:29

    This is a complicated problem. The reason is that the background-image can be applied to the element by using a separate CSS file. This way parsing all the objects in the DOM and checking for their background-image will not work.

    One of the ways I can see is to create a simple HttpHandler which will work on all the type of images. When images are referenced inside the CSS file they are downloaded as a separate entity. This means HttpHandler will be able to capture the type of the image and then execute on it.

    Maybe a server side solution is the best way to go on this one!

    0 讨论(0)
  • 2020-12-29 12:30

    get collection elements which have inline background styles

    document.querySelectorAll('[style*="background"]')
    
    0 讨论(0)
  • 2020-12-29 12:33

    I needed this function, but they were all too heavy unfortunately. So thinking about it, here is my solution, if it can help.

    function getAllBgInHtml()
    {
        return document.body.innerHTML.match(/background-image.+?\((.+?)\)/gi).map(function(e){ return ((e.match(/background-image.+?\((.+?)\)/i) || [])[1] || '').replace(/&quot;|"/g,'') });
    }
    

    Does not work for background image in external stylesheet.

    0 讨论(0)
  • 2020-12-29 12:36

    One of the way is to look through all document object and get there styles. Then test style.background attribute on "url(" string and if it matches then get path between "url(" and ")" and put it into array. Algorithm for JS. Try to do it yourself. Will find troubles - came with code.

    0 讨论(0)
  • 2020-12-29 12:37

    Here's a way to check what background urls there are in the styles on the page (look Ma, no jQuery):

    window.npup = (function (doc) {
      var sheets = doc.styleSheets;
      var hash = {}, sheet, rules, rule, url, match;
      // loop the stylesheets
      for (var sheetIdx=0, sheetsLen=sheets.length; sheetIdx<sheetsLen; ++sheetIdx) {
        sheet = sheets[sheetIdx];
        // ie or w3c stylee rules property?
        rules = sheet.rules ? sheet.rules : sheet.cssRules;
        // loop the rules
        for (var ruleIdx=0, rulesLen=rules.length; ruleIdx<rulesLen; ++ruleIdx) {
          rule = rules[ruleIdx];
          if (rule.selectorText && rule.style.cssText) {
            // check if there's a style setting we're interested in..
            if (rule.style.cssText.match(/background/)) {
              // ..and if it has an url in it, put it in the hash
              match = /url\(([^)]*)\)/.exec(rule.style.cssText);
              if (match) {hash[match[1]] = true;}
            }
          }
        }
      }
      // return an array of unique urls
      var urls = [];
      for (url in hash) {urls.push(url);}
      // export a getter for what we found
      return {
        getBackgroundUrls: function () { return urls;}
      };
    })(document); // submit reference to the document of interest
    

    With this on the page you can get an array of urls with npup.getBackgroundUrls(); I did some (superfluos?) commenting in the code that explains how it goes about. It doesn't grab inlined styles, but I think that was no problem for you? Styles in external sheets and in <style> tags on the page are scavenged.

    The routine is easy to modify if you would like to keep a count, or keep associations to the actual rules that an url was found in etc.

    0 讨论(0)
提交回复
热议问题