Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using image URL with parentheses as background with jQuery

I'm loading a bunch of files as background-image using jQuery and I have this image that will simply not load. The browser does not throw any error.

I know the problem is that the file name contain parentheses (), but I've tried all sorts of things and nothing works. This is not a duplicate from this question as the solution there does not work.

This is the URL of the file

http://oceanhotelsimages.com/uploads/200x150_Privilege_Lounge_OBS_05_(1).jpg

I've tried different things in Javascript with no luck.

unescape(str);

encodeURI(str)

Even directly replacing the characters

str.replace("(", "%28");
str.replace(")", "%29");

Even by pure desperation

str.replace("(", "(");
str.replace(")", ")");

Help me StackOverflow, you are my only hope.

Please do not suggest to change the file name. I do not have access to that file.

like image 864
Pier Avatar asked Apr 10 '15 02:04

Pier


1 Answers

The solution was easier than I thought. I was missing the quotes when declaring the url of the CSS property.

$(this).css({
    "background-image": "url('" + someString + "')"
});
like image 113
Pier Avatar answered Sep 19 '22 01:09

Pier