Forum Moderators: open

Message Too Old, No Replies

how does one write a semi-colon?

         

a440guy

4:08 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



I want to write some JavaScript to another window. When I tried the code below, I got an "unterminated string" error. Apparently, the semi-colon embedded in the string that I'm trying to write is being interpreted as the end-of-statement. I have tried escaping the semi-colon with one, two, and four backslashes. I have tried using the JavaScript escape and unescape functions. I have tried the technique described in [webmasterworld.com...] . None of these has worked. I know that I don't actually need the semi-colon if I put each statement on a separate line, but now I just want to learn how to do this in case I _must_ write one.

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();
}

mattglet

6:46 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have not tried this, but how about:

w.document.write("<script>window.focus()" + unescape('%3B') + "</script>\n");

Hanu

6:59 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



I don't think it's the semicolon but you can put any character into a literal string by using the \xHH sytax. HH stands for a two-digit hexadecimal character code. The character code for the semicolon is 59 or, in hexadecimal notation, 3B.

w.document.write("<script>window.focus()\x3B</script>\n");

a440guy

7:58 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



mattglet and Hanu,

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>

Bernard Marx

7:22 am on Sep 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



w.document.writeln('<\/scr'+'ipt>'); 

Hanu

9:45 am on Sep 23, 2004 (gmt 0)

10+ Year Member



Aha!

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.

a440guy

12:21 pm on Sep 23, 2004 (gmt 0)

10+ Year Member



Thanks! Seems obvious now. I've written HTML with JavaScript before, but this is the first time I've tried to write JavaScript to the page.

Bernard Marx

2:46 pm on Sep 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hanu's right. There is nothing logically wrong with your code. The browser shouldn't parse the quoted string as HTML before it writes it to the page, but some do, so that's the generally used workaround. I've found that <\/script> works, but while we're at it we might as well split the tag just to be doubly sure.