Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets in javascript background url

Tags:

javascript

css

I have a problem with a plugin I am using, where I need to assign a background through jquery.

I get the following url back from the database:

http://www.website.com/upload/projects/25/opbouw_(10)_(800x600).jpg 

And it is not displaying this image because of the brackets in the url. I can not change this url because a client hast uploaded it. Before you point out the security issues about this: I did not wrote it and I can not change it either. So I'll have to deal with this, unfortunately.

I have found a similar question about spaces in a javascript url, but you can use encodeURIComponent and encodeURI to solve that problem, however, the () are unescaped characters and will not be formatted properly.

My question is, what am I supposed to do to make sure the image shows properly? Some kind of Regex? (I suck at Regex btw).

Thanks! :-)

Edit:

some javascript code...

iw.css({'background-image':'url('+ent.data("src")+')','backgroundPosition':'50% 49%', 'backgroundSize':'cover', 'background-repeat':'no-repeat'});

ent.data("src") is the url I get back

like image 347
Bananam00n Avatar asked May 23 '26 17:05

Bananam00n


1 Answers

The "correct" way to define a background image is like so:

url("/path/to/image.jpg")

Note the use of quotes. People always seem to forget the quotes... They are optional in most cases, but as seen here they are required for proper parsing. Add them.

like image 170
Niet the Dark Absol Avatar answered May 26 '26 07:05

Niet the Dark Absol