To see the problem, look at this top line in the script element:
<script type="text/javascript"> document.write('<script src="set1.aspx?v=1234" type="text/javascript"></script>'); </script>
So, the HTML parser comes in and sees the opening tag <script>. Inside the <script> parsing of the normal <tag is disabled (in terms of SGML, the element has the contents of CDATA). To find where the script block ends, the HTML parser searches for the corresponding close </script> tag.
The first one he finds is inside the string literal. An HTML parser cannot know that it is inside a string literal, because HTML parsers do not know anything about JavaScript syntax, they only know about CDATA. So you actually say:
<script type="text/javascript"> document.write('<script src="set1.aspx?v=1234" type="text/javascript"> </script>
That is, an open string literal and an incomplete function call. This leads to JavaScript errors, and the desired script tag is never written.
A common attempt to solve a problem:
document.write('...</scr' + 'ipt>');
This is still technically incorrect (and will not be verified). This is because, in SGML, the character sequence that ends with the CDATA element is not actually "</tagname>, but simply" </ is the sequence that is still present in the line above. Browsers, as a rule, are more forgiving and in practice this will allow.
Probably the best solution is to exit the sequence. There are several possibilities, but the simplest is to use JavaScript string literal scripts ('\ xNN'):
document.write('\x3Cscript src="set1.aspx?v=1234\x26w=5678" type="text/javascript"\x3E\x3C/script\x3E');
The above excludes all the '<,'> and '& characters, which not only stops the "</ sequence appearing in the line, but also allows you to insert it into the XHTML script block without causing errors.
(There is no such thing as a CDATA element in XHTML, so these characters will have the same meaning as in the normal content, and the string '<script>' inside the script block will actually create a nested script element! You can resolve <> & in the block XHTML script using the <[CDATA [] section, but this is a bit ugly and it is usually best to avoid using these characters in an inline script.