1

I have made a small plugin which checks to see if a element(s) background image has loaded.

In doing this I needed to extract the URL of the background image, I have managed to do it when there is just one background image:

var bgURL = $('#myelement').css('background-image').replace(/^url\(["']?/, '').replace(/["']?\)$/, '');

However I want to make my plugin support elements with multiple background images

Here is a code pen example of the code in action if it helps.

It would be good if it worked for any css format e.g. both:

background-image: url(sheep.png), url(betweengrassandsky.png);
background-position: center bottom, left top;
background-repeat: no-repeat;

and

background: url(sheep.png) center bottom no-repeat, url(betweengrassandsky.png) left top no-repeat;

Ideally the result would be an array of the urls (e.g. 0 => ['sheep.png'], 1 =>['betweengrassandsky.png'])

Thanks in advance guys.

5
  • You don't mean multiple background images on the same element, do you? Commented Oct 30, 2014 at 10:17
  • No, I do mean on the same element Commented Oct 30, 2014 at 12:34
  • 1
    You could split the property on , and run your regex on each in the array. Commented Oct 30, 2014 at 13:35
  • Good point that would probably do it. Commented Oct 30, 2014 at 14:58
  • I wasn't sure if you were asking for a solution for multiple background images on the same element, or display only one load time for all 3 background images. But yeah, split should work. Good luck. Commented Oct 30, 2014 at 15:06

2 Answers 2

2

Thanks to artm for his idea.

I split the background image by ", " and then run a foreach loop over the array running my existing replace methods.

This is essentially what I did:

var array = [];
$.each($('#myElement').css('background-image').split(', '), function(key, value){
    array.push(value.replace(/^url\(["']?/, '').replace(/["']?\)$/, ''));
});

Here is a working code pen:

http://codepen.io/catmull/pen/Hhufw

You can download the plugin that detects page load from here as well

http://catmull.uk/code-lab/background-image-loaded/

Sign up to request clarification or add additional context in comments.

Comments

1

Obviously you came to the same idea before I can submit, but anyway, I have already written the code... :) Actually, if you use "url" for your split function, you don't have to check if the background has one url or many - idx will be always at least 0 and 1, and you will need to catch always the entries >0.

// Loop through elements
    this.each(function(){
        var Obj = $(this);
        $.each( Obj.css('background-image').split("url"), function(idx, value){
            if (idx>0){
                 //The stuff you're doing with the img tag
                 $('<img/>').attr('src', value.replace( /\(["']?/,'' ).replace( /["']?\),?/,'' )).load(function(){
                     $(this).remove(); // prevent memory leaks
                     settings.afterLoaded.call(Obj);
                 });                }
        });
    });

Comments

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.