2

There are couple of ways to load image src urls using javascript, like using document.images or by selecting all img tags and get srcs.

However I can't figure out a way to image urls used within css.

For example, if the webpage has following css code, it loads bg.png, but I can't get that url using methods I mentioned above.

.bg {
  background-image: url('bg.png');
}

Anyone has an idea how to get all these urls used within css?

11
  • 2
    document.styleSheets search the rules in each sheet? Commented Jan 21, 2021 at 22:25
  • 4
    Use getComputedStyle. Will it help you? - stackoverflow.com/questions/2104149/… Commented Jan 21, 2021 at 22:26
  • @evolutionxbox is it possible to access every individual stylesheet rules? Commented Jan 21, 2021 at 22:28
  • 1
    @emil, but you can iterate over all the elements using the forEach() method or the for loop + the method to which I gave you the link. Commented Jan 21, 2021 at 23:19
  • 1
    one other idea - the Resource Timing API collects data on outbound requests. Perhaps depending on your application you could use that to parse outbound image requests? that will catch everything mind, not just bg images but inline ones as well. calendar.perfplanet.com/2012/… Commented Jan 22, 2021 at 9:32

2 Answers 2

1

The Resource Timing API collects data on outbound requests, should leave capacity to collect images in both CSS and inline styles performantly.

Haven't tested this, but something akin to this should help you get started:

if ( !('performance' in window) ||
                 !('getEntriesByType' in window.performance) ||
                 !(window.performance.getEntriesByType('resource') instanceof Array)
                 ) {
                      alert('unsupported');
         } else {
            window.addEventListener('load', function() {
               var resources = window.performance.getEntriesByType('resource');
               for(var index in resources) {

                  for(var properties in resources[index]) {
                      console.log(properties);
                      console.log(resources[index][properties]);
                  }
                
               }
            });
Sign up to request clarification or add additional context in comments.

Comments

1

something like this:

  1. Loop all stylesheet rules
  2. grab the document element from the stylesheet
  3. find the background Image

var sSheetList = document.styleSheets;
    for (var sSheet = 0; sSheet < sSheetList.length; sSheet++)
    {
        var ruleList = document.styleSheets[sSheet].cssRules;
        for (var rule = 0; rule < ruleList.length; rule ++)
        {
           if (rule.style.cssText.match(/background/)) {
           var selectorText = ruleList[rule].selectorText );
           var img = document.getElementsByClassName(selectorText);
           var style = img.currentStyle || window.getComputedStyle(img, false);
           if( style.backgroundImage ) {
               var bg = style.backgroundImage.slice(4, -1).replace(/"/g, "");
 //add to array here or whatever.
           }
           }
          
        }
    }

1 Comment

Thanks for your answer @Squiggs. I think you can't get rules if stylesheets is external though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.