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?
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.
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.
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.
Try this:
var img = document.getElementById('widgetField'),
style = img.currentStyle || window.getComputedStyle(img, false),
bi = style.backgroundImage.slice(4, -1);
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;
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, '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With