Pier
Pier

Reputation: 10817

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.

Upvotes: 11

Views: 4033

Answers (1)

Pier
Pier

Reputation: 10817

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 + "')"
});

Upvotes: 26

Related Questions