Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encodeURI function doesn't encode parenthesis “( )”

Tags:

jquery

css

I'm trying to load a background image under a js code as mentioned below:

$('.main-banner').css('background-image', "url("+encodeURI(element.image)+")");

Unfortunately, if the image name element.image contains some parenthesis: ( or ) this will cause a problem, and the image will not load.

like image 268
famas23 Avatar asked May 09 '26 13:05

famas23


1 Answers

encodeURI will not encode the parentheses, Try to replace them manually like :

const url = element.image.replace("(", "%28").replace(")", "%29");
$('.main-banner').css('background-image', "url("+encodeURI(url)+")");

Using the encodeURI method before the replacement will be like :

const url = encodeURI(element.image).replace("(", "%28").replace(")", "%29");
$('.main-banner').css('background-image', "url("+url+")");
like image 107
Zakaria Acharki Avatar answered May 12 '26 05:05

Zakaria Acharki