how to create a MultiLine string

Discussion in 'Javascript' started by κÖлª, Aug 24, 2007.

  1. κÖлª

    κÖлª Guest

    I want to create a multiline string that can works in ie and
    firefox,
    who have a better idea ?
     
    κÖлª, Aug 24, 2007
    #1
    1. Advertising

  2. κÖлª

    Evertjan. Guest

    =?gb2312?B?zrrW0Luq?= wrote on 24 aug 2007 in comp.lang.javascript:

    > I want to create a multiline string that can works in ie and
    > firefox,


    Good for you.

    > who have a better idea ?


    Go to the beach?

    ============================

    Please state your real question. Is this it?

    "Can I write a javascript stringvariable containing more lines?"

    Yes you can.

    "Could you give me an example?"

    <script type='text/javascript'>

    var myString = 'Hello<br>World.<br><br>This is a test.';
    document.write(myString);

    var myOtherString = 'Hello\nWorld.\n\nThis is another test.';
    alert(myOtherString);

    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 24, 2007
    #2
    1. Advertising

  3. κÖлª

    Guest Guest

    I want to define a multiline string.

    like:

    var html = "<html>
    <body>
    </body>
    </html>
    .....
    "
    but i don't want to use '\'
     
    Guest, Aug 24, 2007
    #3
  4. κÖлª

    Evertjan. Guest

    hong hua wrote on 24 aug 2007 in comp.lang.javascript:

    > I want to define a multiline string.


    Are you responding on a previous posting? [Re: ..]

    Please always quote on usenet!

    > like:
    >
    > var html = "<html>
    > <body>
    > </body>
    > </html>
    > ....
    > "


    Why do you want to do that, Hua?

    > but i don't want to use '\'


    Why don't you want to do that?

    How could you use '\' for that?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 24, 2007
    #4
  5. κÖлª

    Guest Guest

    if i want to store the following html code in a variable(javascript of
    course) ,
    Do you have a better idea?

    ================== HTML Start ======================
    > I want to define a multiline string.


    Are you responding on a previous posting? [Re: ..]

    Please always quote on usenet!

    > like:


    > var html = "<html>
    > <body>
    > </body>
    > </html>
    > ....
    > "

    <script language="javascript">
    alert("hello world");
    </script>
    ================= HTML End =======================
     
    Guest, Aug 24, 2007
    #5
  6. wrote:
    > I want to define a multiline string.
    >
    > like:
    >
    > var html = "<html>
    > <body>
    > </body>
    > </html>
    > ....
    > "
    > but i don't want to use '\'


    var html = "<html>" +
    "<body>" +
    "<\/body>" +
    "<\/html>";

    http://javascript.crockford.com/
     
    Douglas Crockford, Aug 24, 2007
    #6
  7. On Aug 24, 12:23 am, Douglas Crockford <> wrote:
    > wrote:
    > > I want to define a multiline string.

    >
    > > like:

    >
    > > var html = "<html>
    > > <body>
    > > </body>
    > > </html>
    > > ....
    > > "
    > > but i don't want to use '\'

    >
    > var html = "<html>" +
    > "<body>" +
    > "<\/body>" +
    > "<\/html>";


    Why escape the '/' on the closing tags? I have only read that is
    necessary for script tags. Do HTML parsers exist that have a problem
    with those closing tags or are you just being conservative?

    I find it a easier to use single quotes when quoting HTML because then
    attribute double quotes don't need escaping.

    "<li id=\"foo\">"
    '<li id="foo">'

    Peter
     
    Peter Michaux, Aug 24, 2007
    #7
  8. κÖлª

    Evertjan. Guest

    Douglas Crockford wrote on 24 aug 2007 in comp.lang.javascript:

    > wrote:
    >> I want to define a multiline string.
    >>
    >> like:
    >>
    >> var html = "<html>
    >> <body>
    >> </body>
    >> </html>
    >> ....
    >> "
    >> but i don't want to use '\'

    >
    > var html = "<html>" +
    > "<body>" +
    > "<\/body>" +
    > "<\/html>";
    >
    > http://javascript.crockford.com/


    But Douglas, she does not want to use "\"!

    var sl = String.fromCharCode(47);

    var html = '<html>' +
    '<body>' +
    '<'+sl+'body>' +
    '<'+sl+'html>';

    alert(html);


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 24, 2007
    #8
  9. Douglas Crockford wrote:
    > wrote:
    >> I want to define a multiline string.
    >> [...]
    >> but i don't want to use '\'

    >
    > var html = "<html>" +
    > "<body>" +
    > "<\/body>" +
    > "<\/html>";


    I would always prefer

    var html = new Array(
    "<html>",
    " <body>"
    " <\/body>",
    "<\/html>"
    ).join("\n");

    above that.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Aug 24, 2007
    #9
  10. Peter Michaux wrote:
    > On Aug 24, 12:23 am, Douglas Crockford <> wrote:
    >> wrote:
    >>> I want to define a multiline string.
    >>> like:
    >>> var html = "<html>
    >>> <body>
    >>> </body>
    >>> </html>
    >>> ....
    >>> "
    >>> but i don't want to use '\'

    >> var html = "<html>" +
    >> "<body>" +
    >> "<\/body>" +
    >> "<\/html>";

    >
    > Why escape the '/' on the closing tags? I have only read that is
    > necessary for script tags.


    Your information is inaccurate. CDATA content ends at an ETAGO delimiter,
    no matter the element.

    http://www.w3.org/TR/html4/types.html#type-cdata

    > Do HTML parsers exist that have a problem with those closing tags


    Yes, the W3C Validator's SGML parser being one of them.

    > or are you just being conservative?


    Maybe that as well. It is not necessary to escape ETAGOs if they do not
    occur within `script' element CDATA content. So the said snippet has the
    best chance to work everywhere.

    > I find it a easier to use single quotes when quoting HTML because then
    > attribute double quotes don't need escaping.
    >
    > "<li id=\"foo\">"
    > '<li id="foo">'


    Add me. However, it is perfectly Valid to use apostrophes ("single quotes")
    as attribute delimiter as well, even in X(HT)ML.

    (Unfortunately, the SGML spec is not available free of charge at ISO.)
    http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
    http://www.w3.org/TR/REC-xml/#NT-AttValue


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Aug 24, 2007
    #10
  11. κÖлª

    Steve Swift Guest

    Douglas Crockford wrote:
    > var html = "<html>" +
    > "<body>" +
    > "<\/body>" +
    > "<\/html>";


    Aha! If we can create a variable with a multiline string (using the OP's
    nice description) then I wonder if I can use it to set a TITLE
    attribute, and if the resulting tooltip in Opera/Firefox will be multiline?

    This will stretch/extend my meagre JavaScript skills.

    --
    Steve Swift
    http://www.swiftys.org.uk/swifty.html
    http://www.ringers.org.uk
     
    Steve Swift, Aug 24, 2007
    #11
  12. Steve Swift wrote:
    > Douglas Crockford wrote:
    >> var html = "<html>" +
    >> "<body>" +
    >> "<\/body>" +
    >> "<\/html>";

    >
    > Aha! If we can create a variable with a multiline string (using the OP's
    > nice description)


    ...., which would be one including "\n", "\r\n" or "\r", ...

    > then I wonder if I can use it to set a TITLE attribute, and if the
    > resulting tooltip in Opera/Firefox will be multiline?


    No. Opera ignores "\n", and Firefox includes a space for it. Both include
    a space for "\r". Tested in Opera 9.23.8808 and Firefox 2.0.0.6 on Windows
    XP Service Pack 2.

    Note that the HTML 4.01 Specification does not say the `title' attribute
    value has to be displayed as a tooltip in a conforming UA.

    http://www.w3.org/TR/REC-html40/struct/global.html#adef-title

    If you need a tooltip, use a styled element.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Aug 24, 2007
    #12
  13. Thomas 'PointedEars' Lahn wrote:
    > Peter Michaux wrote:
    >> I find it a easier to use single quotes when quoting HTML because then
    >> attribute double quotes don't need escaping.
    >>
    >> "<li id=\"foo\">"
    >> '<li id="foo">'

    >
    > Add me. However, it is perfectly Valid to use apostrophes ("single quotes")
    > as attribute delimiter as well, even in X(HT)ML.


    That should have been "attribute _value_ delimiter".

    > (Unfortunately, the SGML spec is not available free of charge at ISO.)


    However, the SGML productions provide a fair idea:

    http://www.w3.org/MarkUp/SGML/productions.html
    http://www.w3.org/MarkUp/SGML/productions.html#prod32


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Aug 24, 2007
    #13
  14. Thomas 'PointedEars' Lahn wrote:
    > Peter Michaux wrote:
    >> On Aug 24, 12:23 am, Douglas Crockford wrote:

    <snip>
    >>> var html = "<html>" +
    >>> "<body>" +
    >>> "<\/body>" +
    >>> "<\/html>";

    >>
    >> Why escape the '/' on the closing tags? I have only read
    >> that is necessary for script tags.

    >
    > Your information is inaccurate.


    Probably more superficial than inaccurate. To date there have been no
    reports of an browsers that actually terminated CDATA SCRIPT element
    contents just on the first occurrence of ETAGO, they seem to all wait
    for ETAGO followed by (case folded) 'SCRIPT'. But, as usual, a failure
    to observe (or observe and report) is not an indicator of the
    non-existence of the thing that has not been observed. There may be, in
    the past, currently or in the future, a browser that does behave
    precisely as described in the HTML 4 specifications and will terminate
    CDATA at the very first ETAGO, and HTML authors have no justification
    for being surprised when/if that does happen.

    > CDATA content ends at an ETAGO delimiter,
    > no matter the element.
    >
    > http://www.w3.org/TR/html4/types.html#type-cdata


    That would be in HTML, according to the HTML speciation. The SGML
    specification seems to have a different attitude, where it says (ISO
    8879, Appendix B.13.1.1):-

    | If an element contains declared character data, it cannot
    | contain anything else. The markup parser scans it only to
    | locate an ETAGO or NET; other markup is ignored. Only the
    | correct end-tag (or that of an element in which this element
    | is nested) will be recognised.

    - which says that although the parser is looking for ETAGO it is only
    interested in them if they start the "correct end-tag", or the end-tag
    of a containing element. Given that the closing SCRIPT tag is not
    optional (in HTML) it could be suggests that a hypothetical SGML parser
    would skip '</'s until it found </script>. On the other hand it may be
    that if the opening script was in, say, the HEAD the character sequence
    '</head>' might be taken as "that of an element in which this element is
    nested" and used to terminate the CDATA.

    On the whole it has got to be safest to break up all occurrences of
    ETAGO inside CDATA in HTML documents, and so satisfy all interpretations
    of all applicable documents.

    (It is also worth noting that ISO 8879 Appendix B is explanatory text
    not reference.)

    >> Do HTML parsers exist that have a problem with those
    >> closing tags

    >
    > Yes, the W3C Validator's SGML parser being one of them.
    >
    >> or are you just being conservative?

    >
    > Maybe that as well.


    There is no harm in being conservative here.

    > It is not necessary to escape ETAGOs if they do not
    > occur within `script' element CDATA content.


    Aren't they just as potentially problematic in any other element's CDATA
    contents (STYLE?)

    > So the said snippet has the
    > best chance to work everywhere.
    >
    >> I find it a easier to use single quotes when quoting HTML
    >> because then attribute double quotes don't need escaping.
    >>
    >> "<li id=\"foo\">"
    >> '<li id="foo">'

    >
    > Add me.

    <snip>

    As do I, but not for that reason. It is a habit I acquired writing JSP,
    where javascript and Java may appear together. I wanted the two to be as
    visual distinct as possible (even though the syntax highlighting rules
    for JSP apply the same rules to all code seen) and given that Java
    string literals must be delimited with double quotes using single quotes
    with javascript meant that if chunks of code contained sequences of text
    characters in one color they were Java, and another color meant
    javascript. A very immediate indicator of which code context you are
    looking at in a JSP.

    <snip>
    > (Unfortunately, the SGML spec is not available free
    > of charge at ISO.)

    <snip>

    Anyone considering paying for a copy of an ISO specification would be
    well advised to get their copy in electronic form. Although I much
    prefer reading documents from paper (rather than a computer screen) the
    quality of the ISO printed documents is not particularly good (read
    'quite bad', even (ironically for the ISO) 'substandard') and it would
    not take much work to achieve a superior hard copy from a printout of
    the electronic version.

    Richard.
     
    Richard Cornford, Aug 26, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Shevitz
    Replies:
    3
    Views:
    305
    Danny Shevitz
    Apr 30, 2008
  2. Robert Dailey

    Need cleanup advice for multiline string

    Robert Dailey, Aug 11, 2009, in forum: Python
    Replies:
    62
    Views:
    1,259
    Stefan Behnel
    Aug 24, 2009
  3. lallous

    Building a multiline string

    lallous, Feb 4, 2010, in forum: Python
    Replies:
    5
    Views:
    617
  4. dale zhang
    Replies:
    8
    Views:
    430
    Tintin
    Nov 30, 2004
  5. Peng Yu
    Replies:
    2
    Views:
    140
    Tad J McClellan
    Jun 24, 2008
Loading...

Share This Page