Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the background image URL of a div via JavaScript?

I've found plenty of information on how to change the background image of a div using JavaScript, but I am trying to use JavaScript to determine which background image is being displayed. The code to set the image goes like this:

document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)";

I have tried every combination I can think of to access the background image url, but so far no dice:

alert(document.getElementById("widgetField").style.backgroundImage.url);  - returns Undefined
alert(document.getElementById("widgetField").style.backgroundImage);  - empty response
alert(document.getElementById("widgetField").style.background);
alert(document.getElementById("widgetField").style.background.image);
alert(document.getElementById("widgetField").style.background.url);
alert(document.getElementById("widgetField").style.background.image.url);
alert(document.getElementById("widgetField").style.background.value);
alert(document.getElementById("widgetField").style.background.image.value);
alert(document.getElementById("widgetField").style.background.image.value);
alert(document.getElementById("widgetField").style.backgroundImage.value);

Does anyone know how to do this? Is it possible?

BTW, here is the way the image is being set in CSS in the beginning:

#widgetField {
    width: 290px;
    height: 26px;
    background: url(../images/datepicker_closed.png);
    overflow: hidden;
    position: relative;
}

UPDATE:

If I run the following, it works:

document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)";
alert(document.getElementById("widgetField").style.background);

However, I cannot seem to access the URL property until it has been set by JavaScript, even though it is already defined in the CSS file. Is there a reason why the raw CSS setting is not accessible?

like image 406
user77413 Avatar asked Jan 20 '10 19:01

user77413


People also ask

How do you code a background image in Javascript?

backgroundImage = "url('image. png')"; You need to put the relative path to the image from your web page's folder. If the image is located in the same folder, then you can reference it directly inside the url() function as in the code above.

How do you check if a URL is an image?

To check if a url is an image, call the test() method on a regular expression that matches an image extension at the end of a string, e.g. . png or . jpg . The test() method will check if the url ends with an image extension and will return true if it does.

How do I get an image URL in CSS?

Usage is simple — you insert the path to the image you want to include in your page inside the brackets of url() , for example: background-image: url('images/my-image. png'); Note about formatting: The quotes around the URL can be either single or double quotes, and they are optional.


3 Answers

Try this:

var img = document.getElementById('widgetField'),
style = img.currentStyle || window.getComputedStyle(img, false),
bi = style.backgroundImage.slice(4, -1);
like image 114
Mic Avatar answered Oct 17 '22 16:10

Mic


You're setting the background property, background and backgroundImage are two seperate properties which is why backgroundImage is empty after setting background. If you want to access just the url part of the background property, you can use the following code:

var wfBg = document.getElementById("widgetField").style.background;
var wfBgUrl = wfBg.match(/(url\(['"]?([^)])['"]?\))/i);

if (wfBgUrl)
{
    // Add your code here. wfBgUrl[1] is full string including "url()", 
    // wfBgUrl[2] would be just the url inside the parenthesis
}

For styles set by css documents:

if (window.getComputedStyle) // For standards compliant browsers
    var wfBg = window.getComputedStyle(
        document.getElementById("widgetField")
    ).getPropertyValue("background");
else // for IE
    var wfBg = document.getElementById("widgetField").currentStyle.background;
like image 3
Andy E Avatar answered Oct 17 '22 17:10

Andy E


The CSS backgorund prop may also include additional attributes as seen on https://developer.mozilla.org/en-US/docs/Web/CSS/background

background: no-repeat url("../../media/examples/lizard.png")

Using a simple img.style.background.slice(4, -1) will give wring results in these cases.

Why not use

/url\(".+"\)/i.exec(img.style.background)[0].replace(/^url\("/i, '').replace(/"\)$/i, '')

where /url\(".+"\)/i is a regex that matches the url("...") part, of which you take the first match with [0] and then replace the initial url(" with replace(/^url\("/i, '') and the ending ") with replace(/"\)$/i, '')

like image 1
Giorgio Avatar answered Oct 17 '22 17:10

Giorgio