Help: Imbedded </script> within a document.write()

M

Mike Daniel

I am attempting to use document.write(pageVar) that displays a new html page
within a pop-up window and the popup is failing. Also note that pageVar is
a complete HTML page containing other java scripts.

Being a javascript newbie and after significant testing, I suspect that the
document.write fails after finding a </script> within pageVar.

Does a trick exist that enables one to slightly alter pageVar whereby
enabling document.write(pageVar) to display a complete HTML page that
contains other javascripts?

Thanks,
Mike.
 
D

Douglas Crockford

I am attempting to use document.write(pageVar) that displays a new html page
within a pop-up window and the popup is failing. Also note that pageVar is
a complete HTML page containing other java scripts.

Being a javascript newbie and after significant testing, I suspect that the
document.write fails after finding a </script> within pageVar.

Put a backslash in front of the slash.

document.write('<\/script>');

JavaScript will happily ignore the backslash, which is at the same time
preventing the browser's hopeless confusion.

jslint catches mistakes like these.

http://www.crockford.com/javascript/lint.html
 
G

Greg

If I understand you correctly, then this is the same issue I encountered
last year, and I managed to work around it.

Write it in several parts,

response.write("<scr")
response.write("ipt>")

This is ugly and terrible, but it works.

Gets really messy when you've got vbscript writing javascript which is
writing html dynamically but... sometimes we have to play the cards we
are dealt.

I hope that helps you out.
Greg.
 
L

Lasse Reichstein Nielsen

Mike Daniel said:
I am attempting to use document.write(pageVar) that displays a new html page
within a pop-up window and the popup is failing. Also note that pageVar is
a complete HTML page containing other java scripts.

When you have a script element, it starts at the <script ...> tag and
ends at the *first* occurence of "</" after that. In practice, most
browsers are less picky, and doesn't stop until the first occurence
of "</script>".

So, if you have:
<script type='text/javascript">
var foo = "</script>";
</script>
the script element ends prematurely.

The recommended solution is to write:
var foo = "<\/script>";
This escapes the forward slash, which doesn't matter in Javascript
strings, but seen from the HTML parser's perspective, there is no
longer a "</" too many.


In your case, you have a string containing HTML code to be written.
That code contains script tags. My guess is that one of these
script tags contain a document.write as well, and an HTML script
end tag.

So, whereever you define pageVar, you need to make sure that
when it is written to another page, it writes an escaped slash.

That is, if pageVar contains
' ... "</script>" .... '
(a string to be written on the second page), you must change it
to:
' ... "<\\/script>" ... '
The double backslash means that the string contains a backslash,
and that it is written correctly to the other page.

Good luck
/L
 
M

Mike Daniel

Additional details:

The original HTML document is generated on a remote system via a servlet
requests. The servlet requests from the remote system a HTML pepared menu
and wraps the returned menu with XML, then the servlet requests from the
remote system a HTML constructed report and wraps it with report specific
XML. The same servlet concatenates the two documents together with
appropriate beginning and ending XML tags (then passes it back into Novell's
NPS/eXtend Directory portal servlet). The portal servlet then unwraps (I
think) the XML and sends the combinded HTML document to the browser. The
browser displays the menu on one window and (intentions are) the report is
supposed be displayed in a browser type popup window. The main window
contains the popup javascript. In the same document, the popup javascript
is called and (at this time) the entire HTML document has been inserted into
the <SCRIPT>...document.write("<HTML> ...<script>
....</script>...</HTML>")</script>, and can be seen via the browsers view
source. The pageVar referenced in the original request is this inserted
HTML document wrapped with double quotes. At this time, the HTML document
contains only single quotes as converted by servlet after receiving pageVar
from remote system. My thoughts are to eliminate web browser and javascript
confusion.

My thoughts at this time is to correct the report HTML document's script
immediately after the servlet receives it from the remote system. Now I am
having trouble coding the servlet to appropriately alter </servlet> to
<\\/servlet> due to Java escape code "conflict" originating within the
servlet. I was using pageVar.replaceAll("</script>","<\\/script>"); but
this becomes a <\/script> as seen in the view source. Now the report window
appears but no report data follows. If <\/script> replaces <\\/script>, the
java compiler complains.

What tricks can be used to replace "</script>" to look something
"<\/script>" within the java servlet?

Mike.
 
D

Dr John Stockton

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top