Get URL from background-image property

I am currently using this to get the url from the background-image property:

var url = $(this).find('div').css('background-image'); url = url.substr(4, url.length - 5); 

This works great in some browsers (IE6-9), not that:

 url(http://.com/) 

his

 url("http://.com/) 

Is there a safe way to just get the URL from this property? without having to detect a browser or any other material?

+7
source share
1 answer

You can do:

 url = url.replace(/^url\(["']?/, '').replace(/["']?\)$/, ''); 

This will remove url(' and url(" from the beginning of the line, if present, and ") respectively. ') From the end.

+40
source

All Articles