views:

102

answers:

3

Hi,

I'm trying to add html around div contents that my jquery function returns after the ajax request. It does work and adds a little of the html code I've added but upon submitting the "new" modified data with the addtnl html... it strips out this "&aoe=1&q=90" and other parts of the html code that include the & symbol and //?

var url_contents = $('#url_contents').html();
var url_image = $("#imagesz").find('img').attr('src');
var addtnl = '<br\/><table width="100%" border="0" cellspacing="0" cellpadding="0">tr><td valign="top" align="left">' + url_contents + '<\/td><td valign="top" align="right"><img src="http:\/\/mydomain.com\/thumb\/?src=' + url_image + '&aoe=1&q=90" border="0" style="max-width:125px;"><\/td><\/tr><\/table>';

Have never tried this before but it needs to be setup this way so images are cached on my server and not pulled from the websites my function fetches data from. Not quite sure what I'm doing wrong?

+2  A: 

try changing the & to &amp;:

var url_contents = $('#url_contents').html();
var url_image = $("#imagesz").find('img').attr('src');
var addtnl = '<br /><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="left">' + url_contents + '</td><td valign="top" align="right"><img src="http://mydomain.com/thumb/?src=' + url_image + '&amp;aoe=1&amp;q=90" border="0" style="max-width:125px;"></td></tr></table>';

by the way: i don't think you need to escape the forward slashes (/): instead of \/, you can just write /.

ax
Still the same result... i put an alert(addtnl) to see what the var looks like before its submitted via ajax and it looks perfect but after its submitted to the database everything after the image ext .jpg gets lost / stripped out???
brandon
Below is the result before the ajax...<br /><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="left"><b><a href="http://help.yahoo.com/l/us/yahoo/ysm/sps/" target="_blank" title="Sponsored Search | Sponsored Search Help">Sponsored Search | Sponsored Search Help</a></b><br><font size="1">http://help.yahoo.com/l/us/yahoo/ysm/sps/</font><br></td><td valign="top" align="right"><img src="http://mydomain.com/thumb/?src=http://l.yimg.com/a/i/us/help/propicn/100_help_1.png"></td></tr></table>
brandon
This is the result in the database... <br /><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="left"><b><a href="http://help.yahoo.com/l/us/yahoo/ysm/sps/" target="_blank" title="Sponsored Search | Sponsored Search Help">Sponsored Search | Sponsored Search Help</a></b><br><font size="1">http://help.yahoo.com/l/us/yahoo/ysm/sps/</font><br></td><td valign="top" align="right"><img src="http://mydomain.com/thumb/?src=http://l.yimg.com/a/i/us/help/propicn/100_help_1.png
brandon
there should be code option in comments... attn: stackOverflow
brandon
anyhow all html after .png is gone / stripped out... very odd???
brandon
+2  A: 

Perhaps you need to call encodeURI on the image source attribute

' ...<img src=' + encodeURI('http://mydomain.com/thumb/?src=' + url_image + '&amp;aoe=1&amp;q=90') + ' border="0" style... '
Ray
+2  A: 

You need to URL encode the values in the parameters, then you need to HTML encode the entire URL. If there is no characters that need HTML encoding in the src value, you only need to HTML encode the & characters:

...<img src="http://mydomain.com/thumb/?src=' + encodeURI(url_image) + '&amp;aoe=1&amp;q=90" border="0" style="max-width:125px;">...
Guffa
brandon
You guys on stackOverflow are amazing!
brandon