including new lines in string variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    including new lines in string variables

    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
  • Mick White

    #2
    Re: including new lines in string variables

    mr_burns wrote:
    [color=blue]
    > hi,
    >
    > is it possible to have a string variable to contain new lines. i tried
    > the following but it didnt work:
    >
    > var_string =[/color]

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

    Mick

    Comment

    • mscir

      #3
      Re: including new lines in string variables

      Mick White wrote:[color=blue]
      > mr_burns wrote:[color=green]
      >> is it possible to have a string variable to contain new lines. i tried
      >> the following but it didnt work:
      >>
      >> var_string =[/color]
      >
      > var theString='<h1> Title</h1><br><img src="image.gif> <p>Body text</p>';
      >
      > Mick[/color]

      The second double-quote at the end of the image name is missing.

      var string0 = '<h1>Title</h1><img src="image.gif" ><p>Body text</p>';
      alert(string0);
      var string1 = '<h1>Title</h1>\n<img src="image.gif" >\n<p>Body text</p>\n';
      alert (string1);

      Comment

      • Ivo

        #4
        Re: including new lines in string variables

        "mr_burns" typed:[color=blue]
        > 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.[/color]

        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.)
        [color=blue]
        > 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).[/color]

        \n has the same meaning in javascript. Tip: It is more rewarding and
        motivating to discover such things through experimentation .
        [color=blue]
        > also, is there a limit to the amount of characters that can be
        > contained within a javascript string variable? cheers[/color]

        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



        Comment

        • wasntme

          #5
          Re: including new lines in string variables

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

          Comment

          • Randy Webb

            #6
            Re: including new lines in string variables

            wasntme wrote:[color=blue]
            > might try
            > var_string='<h1 >Title</h1>'
            > +'<img src="image.gif> '
            > +'<p>Body text</p>';
            >[/color]

            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.javas cript FAQ - http://jibbering.com/faq/

            Comment

            • Dr John Stockton

              #7
              Re: including new lines in string variables

              JRS: In article <651c6ea9.04040 81447.4f4538b4@ posting.google. com>, seen
              in news:comp.lang. javascript, mr_burns <bissatch@yahoo .co.uk> posted at
              Thu, 8 Apr 2004 15:47:42 :[color=blue]
              >
              >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>';[/color]

              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.demo n.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)

              Comment

              Working...