Evicted Cache
Evicted Cache

Reputation: 1401

Slashes removed from variable

I have a variable that I am getting from an image's source attribute. When I set that variable as the background of an element using the style tag, all of the slashes are stripped out of it. Here is some of my code:

var logo    =   $('.logo-list li.selected-logo img').attr('src');
$('.stage').append('<div class="doc-header"><div class="product-logo"></div><div class="logo-small" style="background:url('+logo+');"></div></div>');

When I set an alert for the variable 'logo' it looks fine: http://www.example.com/images/img.jpg

But when jQuery appends the code, the background attribute's URL looks like this: http: example.com images img.jpg

For some reason the slashes are getting removed/escaped, I just don't know why. Can anyone shed light on this?

Upvotes: 4

Views: 4625

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68400

Your problem is an extra double quote here "background:"url. Remove the second double quote and should work fine

DEMO

Upvotes: 0

Evicted Cache
Evicted Cache

Reputation: 1401

I was able to fix it by using decodeURIComponent, thanks to @BradM's suggestion.

var logo    =   $('.logo-list li.selected-logo img').attr('src');
var logo    =   decodeURIComponent(logo);

Upvotes: 2

Related Questions