javascriptjqueryurlescaping

URL escape for jQuery


I have the following url:

http://s7w2p3.scene7.com/is/agm/creativprint/a4?&page=3&wid=354&fmt=jpg,rgb&setAttr.img_page3={source=@Embed%28%27/ugc/8423326.tif%27%29}. 

I want to set this as the url of background-image of a div via jQuery('#divID').css or document.getElementByID('divID').style.backgroundImage. It does not work, as I suppose I have to escape some charaters in it. The Javascript global function escape() doesnt do the trick either.

I have set it manually as background-image in my .css file and it works. Any suggestions? I have tried it with other URLs of the format http://s7w2p3.scene7.com/is/agm/creativprint/a4?&page=3&wid=354&fmt=jpg,rgb. That works. So the problem must be in the remaining part: &setAttr.


Solution

  • You might have just forgotten the url() around the CSS property:

    $('#id').css('backgroundImage', 'url("http://s7w2p3.scene7.com/is/agm/creativprint/a4?&page=1&wid=354&fmt=jpg,rgb&setAttr.img_page1={source=@Embed%28%27/ugc/4575272.tif%27%29}")');
    

    It works fine for me that way: http://jsfiddle.net/mPyTJ/1/