0

How can I write a HTML-conform String to a new window using a JSP with inner Javascript?


Our Situation:
- Theres a .jsp-File used by a Tomcat-Server
- The .jsp contains a <script language="Javascript>-Tag
- this script needs to write a complete, HTML-5-valid-String to a new Window using window.open and 'window'.document.write(htmlString)


Our .jsp-File (schematic):
<%@ include file = "..." %> <...some page imports...> <...some scripts...> (e.g. <script type = "text/javascript" src="...."> </script>) <% String strMethod = "foo_Method_Bar"; String strOutput = ""; strOutput = someJavaBean-Call_that_brings_valid_html5_string_with_script-Nodes; %> ... <script language="Javascript"> var strHTML = "<%=strOutput%>"; var win = window.open('','','width=...,height=...'); win.document.write(strHTML); top.close(); win.focus(); </script>

Our Problem (schematic):
- .jsp-File gets called.
- .jsp-File calls a JavaBean and sets a global String-Variable to the HTML-String (including <script>-Tags, DocType-Declarations, etc.)
- .jsp-File replaces all occurrences of the Variable with the obtained String
- javascript (<script>-Tag in the .jsp-File) gets Executed
- <script>-Tag of Javascript is already closed, before 'window'.document.write(strHTML) gets executed, because theres a </script>-Tag in the Javascript e.g.

<script language="Javascript"> var strHTML = "<!DOCTYPE html> <html> ... <script> ... </script> ... </html>; var win = window.open(...); win.document.write(strHTML); </script>



- I've searched for quite some time now and I don't have a clue how to solve this issue.
- Writing the HTML-Sourcecode into the Server-File-System and accessing it later on or something like that would be really, really dirty.
- Part of the Problem is that we (of course) have to use this type of javascript-call in the .jsp-File for historical reasons...;-)
- Is there maybe a way to assign the strHTML-Variable in the .jsp-File by Reference only (e.g. without replacing the <%=strOutput%>-Variable)?

Additional Info:
The HTML-String-Generation is used at various places of our programs, multiple times, so i hoped not to have to escape any special CharacterSequences for this particular Call to work.



Appreciate all your help and ideas on this...;-)

Solved: See also Why split the tag when writing it with document.write()? on this issue.

Community
  • 1
  • 1
doldi04
  • 3
  • 1
  • 6

1 Answers1

0

You MUST escape the closing script in the string <\/script> unless the document.write lives in an external .script

Also remember to close the document, and no spaces in the parameters to window.open

like this

<script type="text/javascript">
  var strHTML = '<!DOCTYPE html> <html> ... <script> ... <\/script> ... </html>';
  var win = window.open("","winname","resizable,scrollbars");
  win.document.write(strHTML);
  win.document.close();
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • The Suggestion seems legit, I've added some additional Info to the Question ... ;-) Thanks anyway, I'll keep it as possible workaround at the back of my mind...xD – doldi04 Sep 10 '14 at 14:13
  • Not sure what you mean. If you need to document.write a script tag, you need to escape the end script tag unless the document.write lives in an external .js file. End of issue. – mplungjan Sep 10 '14 at 14:18
  • 1
    You're right, found some explanation on this here: [Why split the – doldi04 Sep 10 '14 at 14:25