Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with the out.write function

Status
Not open for further replies.

galilee

IS-IT--Management
May 9, 2002
2
0
0
CA
Hi!<br><br>I have a problem with the out.write(String) function. I use Javascripts on the client side that are somewhat generated by a JSP page. Simplified example:<br><br><FONT FACE=monospace>&lt;script&gt;<br>&nbsp;&nbsp;&nbsp;document.all.divTest.body.innerHTML = ' &lt;% out.write(strTest); %&gt;'<br>&lt;/script&gt;</font><br><br>This works fine when strTest is a short string like &quot;Hello World&quot;, but it will not work if it includes when it includes linebreakes etc. Then the client script looks something like this:<br><br><FONT FACE=monospace>&lt;script&gt;<br>&nbsp;&nbsp;&nbsp;document.all.divTest.body.innerHTML = '&lt;table&gt;&lt;tr&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;Test&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;&lt;/table&gt;'<br>&lt;/script&gt;</font><br><br>which obviously generates an error. Can anyone think of a good solution to this problem. Thanks!<br><br>Galilee<br>
 
Galilee,<br><br>&gt; which obviously generates an error<br><br>It's not obvious! Did the result of your out.write() call not produce the expected text in the html file?<br><br>If you are using JSP then you might want to do this:<br><br>&lt;script&gt;<br>&nbsp;&nbsp;&nbsp;document.all.divTest.body.innerHTML = &quot;&lt;%=strTest%&gt;&quot;<br>&lt;/script&gt;<br><br>But I don't believe it solves whatever problem you are having.<br><br>-pete<br><br>
 
It obviously generates an error because a string can span multiple lines in javascript. It will then look for a &quot; at the end of the first line.<br><br>Instead of:<br><br><FONT FACE=monospace>&lt;script&gt;<br>&nbsp;&nbsp;&nbsp;document.all.divTest.body.innerHTML = '&lt;table&gt;&lt;tr&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;Test&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;&lt;/table&gt;'<br>&lt;/script&gt;</font><br><br>I need the output of the write (or another function) to be something like:<br><br><FONT FACE=monospace>&lt;script&gt;<br>&nbsp;&nbsp;&nbsp;document.all.divTest.body.innerHTML = '&lt;table&gt;&lt;tr&gt;\n&lt;td&gt;Test&lt;/td&gt;\n&lt;/tr&gt;&lt;/table&gt;'<br>&lt;/script&gt;</font><br><br>Galilee
 
Dear Galilee,<br><br>Thanks for helping me see the problem. duh!<br><br>Anyway here is what you need the string to look like, I don't know how it is being generated so I'm not sure what it will take for you to make it look like this:<br><br>&quot;&lt;table&gt;&lt;tr&gt;\\n&lt;td&gt;Test&lt;/td&gt;\\n&lt;/tr&gt;&lt;/table&gt;&quot;<br><br>You need to 'escape' all the backslash characters because they are 'escape' characters &lt;redundant&gt; ;o)<br><br>Hope this helps<br>-pete<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top