Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fine however in some browser (IE6-9), instead of it being:

url(http://.com/)

its

url("http://.com/)

Is there a failsafe way that will just get the url from this property? without having to do browser detection or some other stuff?

like image 361
Ozzy Avatar asked Jun 18 '11 17:06

Ozzy


1 Answers

You could do:

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

This will remove url(' and url(" from the beginning of the string if it is present and ") resp. ') from the end.

like image 190
Felix Kling Avatar answered Nov 19 '22 04:11

Felix Kling