java - Writing HTML-String to new window from Javascript in JSP -


this question has answer here:

how can write html-conform string new window using jsp inner javascript?


our situation:
- theres .jsp-file used tomcat-server
- .jsp contains <script language="javascript>-tag
- script needs write complete, html-5-valid-string new window using window.open , '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 javabean , sets global string-variable html-string (including <script>-tags, doctype-declarations, etc.)
- .jsp-file replaces occurrences of variable obtained string
- javascript (<script>-tag in .jsp-file) gets executed
- <script>-tag of javascript closed, before 'window'.document.write(strhtml) gets executed, because theres </script>-tag in 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 quite time , don't have clue how solve issue.
- writing html-sourcecode server-file-system , accessing later on or really, dirty.
- part of problem (of course) have use type of javascript-call in .jsp-file historical reasons...;-)
- there maybe way assign strhtml-variable in .jsp-file reference (e.g. without replacing <%=stroutput%>-variable)?

additional info:
html-string-generation used @ various places of our programs, multiple times, hoped not have escape special charactersequences particular call work.



appreciate , ideas on this...;-)

solved: see why split tag when writing document.write()? on issue.

you must escape closing script in string <\/script> unless document.write lives in external .script

also remember close document, , no spaces in parameters 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> 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -