Reputation: 2280
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.
Upvotes: 3
Views: 2445
Reputation: 67525
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+")");
Upvotes: 3