faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

117 of 179 people (65%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How to document.write SCRIPT sections?

Oct 6th, 2000 06:05
Primal Fear, Martin Honnen,


Inside a client side JavaScript SCRIPT section e.g.
  <SCRIPT>

  </SCRIPT>
you often want to document.write HTML tags and in particular another 
SCRIPT section. To be able to do that you need to escape the closing 
</SCRIPT> tag which is best done with
  document.write('<\/SCRIPT>');
as otherwise the browser will falsely associate the closing </SCRIPT> 
with the outer SCRIPT section. 
The new HTML standard requires escaping any closing tag so it is good 
practice to use
  document.write('<\/BODY>')
  document.write('<\/HTML>')
etc. in general for document.written closing tags.

ADDED BY PRIMAL FEAR:

when you have your javascript in an .js file you can just user

document.write('</SCRIPT>') it won't crash on it. Also if escaping 
doesn't work you can also do it like this:

scriptBegin = "<SCR" + "IPT>";
document.write(scriptBegin);

:)