samedi 18 avril 2015

JavaScript: Sending quotes in a string to style div elements in the DOM, background-image: url('');

For some reason unknown to me, I can't get single quotes in the url('') when I try to set the style.backgroundImage.


I have a div,



<div id="radarDisplay"></div>


that I want to modify with JavaScript after an event (which is working).


I have a URL string,



var imgURL = "http://ift.tt/1Hk6rE9";


Then I modify the string in JavaScript, escaping quotes where I need them:



var bkgdURL = "url(\'" + imgURL + "\')";


In my JavaScript, upon an event, I call:



document.getElementById("radarDisplay").style.backgroundImage = bkgdURL;


No Image....


Looking at the html in my browser, my single quotes in url('') do not appear. It reads:



<div id="radarDisplay" style="background-image: url(http://ift.tt/1Hk6rE9); background-position: 50% 50%; background-repeat: no-repeat;"></div>


I did a console.log(bkgdURL);, and my return was exactly how it should be: url('http://ift.tt/1Hk6rE9')


I've tried it with &quot;, \', and \".


What am I missing here?


Aucun commentaire:

Enregistrer un commentaire