Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unterminated string constant (spanning multiple lines) 2

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
It's been a couple of years, and I'm forgetting something real easy and stupid here... In the code

document.write("Hi, this is to long to fit on one line
so it is on two");

I am getting an unterminated string constant error which I am assuming is coming from that fact that the end " is not on the same line. Please refresh my memory - how the %^&% do you span multiple lines within the quotes?

Thanks in advance
 
Try document.write(&quot;Hi...&quot; + &quot;<br>&quot; + &quot;so...&quot;);

Everything I know I learned from The Grateful Dead and Buffy The Vampire Slayer
Charlie
 
I don't think there is a span_line character in javascript. (eg, in VBscript you would use the _ character at the end of the line)

I guess your options are: put it all on one line, or use two document.writes.

Code:
document.write(&quot;Hi, this is to long to fit on one line &quot;);
document.write(&quot;so it is on two&quot;);


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Actually the backslash will work for that:

alert('this is a string that spans two lines');
 
Can't remember where I found it. Never used it in fact. I prefer to break up the string into smaller substrings and join them together with '+'. Using the backslash will include all the space you would put in for readability.
 
thinking about it.. since the \ is inside the string it must be escaping the next character (in this case it would be a CR). I would guess that the resulting CR is in the string then? Anyone?

The VBscript equivalent is slightly different as its not within the string, eg:
Code:
strBlah = &quot;this is VBscript broken&quot;_
 & &quot; across two lines with the _ char&quot;

And I think there is no equivalent in javascript that you can use that way.. (basically for formatting)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
You don't need one. Because Javascript isn't interpreted line-by-line, there's no need to tell the interpreter to treat the next line as part of this one.

var strMsg = &quot;This &quot; +
&quot;String &quot; +
&quot;Gets &quot; +
&quot;Broken &quot; +
&quot;Up &quot; +
&quot;Over &quot; +
&quot;Many &quot; +
&quot;Lines.&quot;;

alert(strMsg);
 
aha - very good.

So this would work for the original poster perhaps?
Code:
alert(&quot;First part&quot; +
               &quot; and second part&quot;);

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
from
When working with very long strings, tracking bugs such as improper quote nesting or missing escape characters is made easier by breaking the string up into smaller strings. Breaking up a string using the concatenation operator and a carriage return will accomplish the task, but be careful never to use the operator/carriage return pair without closing the string first.


Seems to corroborate our ideas. Still wondering about that \ +CR inside a string though :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I haven't had a chance to try any of the suggested solutions yet, but I did find in one of my reference books (I got desperate and actually cracked open a manual - sacrilege, I know) a definition of document.write as &quot;A command used to write a single line of text to the document&quot;, so I guess there is no way it is &quot;supposed&quot; to be done.
 
build the string up over multiple lines then pass it into document.write once built...

var strMsg = &quot;This &quot; +
&quot;String &quot; +
&quot;Gets &quot; +
&quot;Broken &quot; +
&quot;Up &quot; +
&quot;Over &quot; +
&quot;Many &quot; +
&quot;Lines.&quot;;


document.write(strMsg);

on error goto hell
 
Well, I'll be! I learned something new today. This works

<script>
alert(&quot;test 1,2,3&quot;)
</script>

So does this:
<script>
document.write(&quot;Hi, this is to long to fit on one line so it is on two&quot;);
</script>

Dwarfthrower gets a star!

Adam
 
Well it's been a year and this problem has been sitting in the back of my head the whole time. I finally found a way to have a multiple line string without adding extra characters.
Code:
<script>
function myLongString(){/*

This is a string that
spans multiple lines 
without using
concatenation or 
backslashes!!

*/}

Function.prototype.getComment=function(){
  return this.toString().substring(this.toString().indexOf("/*")+2,this.toString().indexOf("*/"));
}

alert(myLongString.getComment());

</script>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Truly a god amongst men. Well Done. [thumbsup2]

Now for crying out loud, get a hobby or something :)



Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
This IS my hobby! [smile]

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top