Replace image.src with background.image.url

Subscribe to Replace image.src with background.image.url 5 posts, 4 voices

michaeldoo User

I am trying to replace a image.src with the url from it's background image.

var img = document.evaluate("//img[contains(@src, 'clear.cache.gif')]",document,null,9,null).singleNodeValue;
var imgbackground = img.style.background;
img.src = imgbackground;

imgbackground returns "transparent url(show.image.jpg) no-repeat scroll center center"; how do I just get "show.image.jpg" from transparent url instead?

The Html example I am trying to modify is:

<img class="imageViewMainImage" src="clear.cache.gif" style="background: transparent url(show.image.jpg) no-repeat scroll center center; 
height: 500px; width: 500px; visibility: visible; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; 
-moz-background-inline-policy: -moz-initial;" title="Use your mousewheel to navigate the image gallery."/>

Thanks!!! (sorry for my poor newbie code)

 
Yansky Scriptwright

nm. Didn't read the question properly.

 
Avindra Gool... Scriptwright

this should do the trick:

var img = document.evaluate("//img[contains(@src, 'clear.cache.gif')]",document,null,9,null).singleNodeValue;
var imgbackground = img.style.background.match(/url\(([^\)]+)/)[1];
img.src = imgbackground;

cheers.

 
michaeldoo User

@avg, you rock!!!

 
dob Scriptwright

function getstyle(element, property) {
	return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
}
alert(getstyle(img, "background-image"))

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel