Click to See Complete Forum and Search --> : Unterminated string constant
Comike14
September 26th, 2005, 10:37 AM
This is somewhat related to my earlier thread. I keep getting an "unterminated string constant" error when I'm trying to write to another window. This should be a simple thing to fix, but I just can't seem to figure it out. I've tried all of the following to no avail. Please help!
write('<script language=\"javascript\" src=\"/actions/getSupers.js\"></script>');
write("<script language=\'javascript\' src=\'/actions/getSupers.js\'></script>");
write('<script language="javascript" src="/actions/getSupers.js"></script>');
write('<script language=\"javascript\" src=\"/actions/getSupers.js\">\<\/script>');
olivthill
September 26th, 2005, 11:56 AM
Your strings are looking good to me.
Sometimes this error message is caused by a line above, e.g.
write("foo);
write("<script language=\'javascript\' src=\'/actions/getSupers.js\'></script>");
then the program will see a string containing "foo write(" and other strings, the last one being "); which is unterminated.
Or maybe the problem lies in getSupers.js.
Comike14
September 26th, 2005, 03:00 PM
Thanks for taking a look at this. I think I figured it out, but unfortunately I'm not sure why it works...
It seems that the </script> tag somehow makes a difference, so I have to escape the slash. In other words, making it <\/script> solves the string error. Why this needs to be done for the </script> tag, but not the </font>, </input>, or any others I have seen fails to make sense to me... But your point that it may be the result of some earlier code could yet prove to give me some answers. Thanks again.
Dr. Script
September 26th, 2005, 06:55 PM
Well, the solution is very simple. The browser parses the HTML by tags. It looks for <font> tags, and sets everything within them to the specific type, color, or size. When it sees <script> it looks for </script>. When it finds it, the JavaScript parser analyzes the code and executes them like an interpretter. When it gets to the last line, it sees:write("<script language=\'javascript\' src=\'/actions/getSupers.js\'>which results in an error. Escaping the slask with a backslash, like this: \/, will cause the tag to be written as </script>, but the HTML parser not to end the <script> segment of code.
Just as an alternative, you could use DOM to create a script object, define its source and type, and append it to the document's body.
Comike14
September 27th, 2005, 03:54 PM
Works like a charm. Even my other solution of using <\/script> was hanging without some mundane empty loop right after it. Thank you!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.