Forum Moderators: open
So, how does one write a semi-colon using document.write()?
Here is what I want to do:
function viewSel() {
w = window.open('','view','scrollbars,resizable,width=400,height=300');
w.document.write("<script>window.focus();</script>\n");
// more document.write() statements
w.document.close();
}
Both of your techniques worked. But, I now agree that it is probably not the semi-colon that is causing the problem. When I inserted //<!-- and //--> around my JavaScript code, the error went away. Like this:
<script language="javascript" type="text/javascript">
//<!--
previous code goes here
//-->
</script>
Why this should make a difference is a mystery to me. All the browsers that I am using (Mozilla 1.6, Firefox 1.0PR, Netscape 7.2) understand the <script> tag. I'm content to just go ahead without understanding this, but I wish I understood what was going on. For those of you who are interested, I've included all of my code (in the form that causes the error in the JavaScript console) below. The error "unterminated string literal" occurs on the line indicated:
<script language="JavaScript">
window.name = 'main';
var savedSelections = new Object();
function saveSel() {
var i;
var pList = document.queryForm.pathogenid;
for (i=0; i<pList.length; i++) {
if (pList.options[i].selected) {
savedSelections[pList.options[i].text] = pList.options[i].value;
pList.options[i].selected = false;
}
}
}
function viewSel() {
w = window.open('','view','scrollbars,resizable,width=400,height=300');
w.document.writeln('<script>');
w.document.writeln('window.focus();');
w.document.writeln('function deleteChecked() {');
w.document.writeln('main.viewSel()'); // <-- error here immediately after first quote
w.document.writeln('}');
w.document.writeln('</script>');
w.document.writeln('<form id="deleteform">');
w.document.writeln('');
for (p in savedSelections) {
w.document.writeln('<input type="checkbox" name="'+p+'">'+p+'<br>');
}
w.document.writeln('<input type="button" value="Delete Selected Items" onClick="deleteChecked()">');
w.document.write('</form>');
w.document.close();
}
</script>
So the first thing the browser does after having received your page, is to split the page into HTML and JavaScript. The HTML will be processed by the HTML engine and the JavaScript will be processed by the JS engine. The browser sees the <script> tag and then skips everything up to </script>. Because the browser skips the code without properly parsing the JavaScript, it doesn't realize that the first occurrence of </script> is in a string and should not be taken literally. The browser stops there and feeds the JavaScript fragment to the JS engine. The engine gets imcomplete JavaScript ending in a single quotation mark, because that's what comes before the first occurrence of </script>. Hence the error message about an unclosed string.
This is actually a problem of the browser you are using. I can think of a proper parser implementation that doesn't stumble about </script> tags embedded in strings.