PDA

View Full Version : Javascrip in Javascript?


jreamer
11-23-2004, 07:43 PM
The problem:
I'm trying to replace an image with a hyperlinked image. However, the hyperlinked image corresponds to a javascript function - not a URL. Because I am using a simple div/innerhtml replace function, I end up with a call to a javascript function from within another javascript function. Basically I have javascript within javascript. Because of the clash of ' - I get an error. My question is how do I embed the one call within the other so that the 's don't get mixed up?

Here is my code:
<div id="imgZoom"><img src="thumb.gif"></div>

<a href="#"><img src="thumb.gif" onClick="change('imgZoom','<a href=javascript:zoom('large.jpg')><img src=normal.jpg></a>');"></a>

RysChwith
11-24-2004, 09:12 AM
Escape the quotes. This is done by putting a backslash in front of them. It'd look like this:

onClick="change(\'imgZoom\', \'<a href=java script:zoom(\'large.jpg\')><img src=normal.jpg></a>)\';">

Rys

jreamer
11-24-2004, 11:39 AM
Ok - I tried that out and now I'm getting a different error. Here is the actual full code and the error it produces...

<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td align="center"><a class="galleryThumb" href="#"><img src="/public2/ideas/imgs/thumbs/<%=ideaNo%>.gif" alt="" name="" width="70" height="70" border="0" onClick="change('imgZoom','<a href=javascript:qkZoom(\'/public2/ideas/imgs/zooms/img.jpg\',\'Zoom\')><img src=/public2/ideas/imgs/previews/<%=ideaNo%>.jpg border=0 alt=Click to Zoom name=zooma width=225 height=225 border=0 id=zooma></a>');change('txtAbout','<font class=galleryBold><%=ideaTitle%></font><br><font class=galleryDetails><%=ideaCreator%></font>');change('txtImage','<a href=test.htm>');change('txtViews','Number of views:&nbsp;<%=ideaViews%>');change('txtInstructions','<%=ideaInstructions%>');change('txtProducts','<%=ideaProducts%>');" onerror="this.onerror=null;this.src='/public2/imgs/placeholders/70x70.gif';this.alt='Image Coming Soon!';"></a></td>
</tr>
</table>

RysChwith
11-24-2004, 04:40 PM
What error is it producing?

Rys

jreamer
11-24-2004, 04:47 PM
Hooray! Thank you so much Rys - my code was missing a " to close the javascript call. So the escape method \' worked great. That was too much of a headache.