Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > including new lines in string variables

Reply
Thread Tools

including new lines in string variables

 
 
mr_burns
Guest
Posts: n/a
 
      04-08-2004
hi,

is it possible to have a string variable to contain new lines. i tried
the following but it didnt work:

var_string = '<h1>Title</h1>
<img src="image.gif>
<p>Body text</p>';

i know that html will display the same if i was to write the above
into, for example, a <span> tag but i have a very large amount of html
to be contained inside a variable. what i will do first is design it
in dreamweaver and then cut and paste it. as dreamweaver will
automatically format the code with new lines, for me to contain it
inside a variable i will have to put it all into one line (time
consuming and messy). is it possible to do this or hava a special
symbol to declare a new line (i.e. \n in php).

also, is there a limit to the amount of characters that can be
contained within a javascript string variable? cheers


burnsy
 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      04-09-2004
mr_burns wrote:

> hi,
>
> is it possible to have a string variable to contain new lines. i tried
> the following but it didnt work:
>
> var_string =


var theString='<h1>Title</h1><br><img src="image.gif><p>Body text</p>';

Mick
 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      04-09-2004
"mr_burns" typed:
> is it possible to have a string variable to contain new lines. i tried
> the following but it didnt work:
>
> var_string = '<h1>Title</h1>
> <img src="image.gif>
> <p>Body text</p>';
>
> i know that html will display the same if i was to write the above
> into, for example, a <span> tag but i have a very large amount of html
> to be contained inside a variable.


Suggestion: include the HTML as HTML in an invisible span or div. Then
your string can simply be found with:

var var_string = div.innerHTML;

(Pitfall: innerHTML may not be available on browsers you need to
support.)

> what i will do first is design it
> in dreamweaver and then cut and paste it. as dreamweaver will
> automatically format the code with new lines, for me to contain it
> inside a variable i will have to put it all into one line (time
> consuming and messy). is it possible to do this or hava a special
> symbol to declare a new line (i.e. \n in php).


\n has the same meaning in javascript. Tip: It is more rewarding and
motivating to discover such things through experimentation.

> also, is there a limit to the amount of characters that can be
> contained within a javascript string variable? cheers


It has been rumoured IE would choke on strings longer 2000 characters, but I
just tried and all was well even with a variable of 199000 characters.
HTH
Ivo



 
Reply With Quote
 
wasntme
Guest
Posts: n/a
 
      04-09-2004
might try
var_string='<h1>Title</h1>'
+'<img src="image.gif>'
+'<p>Body text</p>';

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-09-2004
JRS: In article < >, seen
in news:comp.lang.javascript, mr_burns <> posted at
Thu, 8 Apr 2004 15:47:42 :
>
>is it possible to have a string variable to contain new lines. i tried
>the following but it didnt work:
>
>var_string = '<h1>Title</h1>
><img src="image.gif>
><p>Body text</p>';


To do what you ask for, insert \n

To do what you might want, insert <br>

To multi-line write a string variable, this may work but is deprecated
var_string = '<h1>Title</h1>_
<img src="image.gif>_
<p>Body text</p>' ;

This is inefficient :
var_string = '<h1>Title</h1>'
var_string += '<img src="image.gif>'
var_string += '<p>Body text</p>' ;

This is effective :
var_string = '<h1>Title</h1>' +
'<img src="image.gif>' +
'<p>Body text</p>' ;


--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      04-09-2004
wasntme wrote:
> might try
> var_string='<h1>Title</h1>'
> +'<img src="image.gif>'
> +'<p>Body text</p>';
>


And it is still missing the " at the end of the image.gif, and doing
string concatenation across several lines like that won't put the
formatting into the source. \n will add a new line to the output:

var_string='<h1>Title</h1>\n<img src="image.gif">\n<p>Body text</p>';


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
[ANN] New RCRchive, including new process dblack@wobblini.net Ruby 29 12-21-2007 04:40 PM
Javadoc including "java.lang.String" instead of just "String" for return values Randolf Richardson Java 2 02-03-2007 07:14 AM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57