Document.write

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

    Document.write

    How do I force the text generated by a document.write command to wrap after
    a specified number of characters?

    Thanks for the help.


  • Michael Winter

    #2
    Re: Document.write

    On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kkrolski@wi.rr .com> wrote:
    [color=blue]
    > How do I force the text generated by a document.write command to wrap
    > after a specified number of characters?[/color]

    Insert \n into the string to force a new line. For example:

    document.write( 'First line\nSecond line');

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Cenekemoi

      #3
      Re: Document.write

      Bonjour à Michael Winter <M.Winter@bluey onder.co.invali d> qui nous a
      écrit :[color=blue]
      > On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kkrolski@wi.rr .com> wrote:
      >[color=green]
      >> How do I force the text generated by a document.write command to wrap
      >> after a specified number of characters?[/color]
      >
      > Insert \n into the string to force a new line. For example:
      >
      > document.write( 'First line\nSecond line');
      >
      > Mike[/color]

      I prefer the "<br>" tag (Html) : document.write( 'First line<br>Second
      line');

      --
      Cordialement, Thierry ;-)

      Comment

      • Michael Winter

        #4
        Re: Document.write

        On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
        <tbaudessonENLE VER@harrysoftwa re.ENVELERcom> wrote:
        [color=blue]
        > Bonjour à Michael Winter <M.Winter@bluey onder.co.invali d> qui nous a
        > écrit :
        >[color=green]
        >> On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kkrolski@wi.rr .com> wrote:
        >>[color=darkred]
        >>> How do I force the text generated by a document.write command to wrap
        >>> after a specified number of characters?[/color]
        >>
        >> Insert \n into the string to force a new line. For example:
        >>
        >> document.write( 'First line\nSecond line');[/color]
        >
        > I prefer the "<br>" tag (Html) : document.write( 'First line<br>Second
        > line');[/color]

        What is most appropriate depends on the context. The OP didn't specify.

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Ken

          #5
          Re: Document.write

          What do I do if the text to be written is a variable and I have no control
          of the content?

          For example
          ....<tr><td colspan=2 align=left><fon t size="2">' + text +
          '</font></td></tr>...

          Ken



          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opr2k73rl8 5vklcq@news-text.blueyonder .co.uk...[color=blue]
          > On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
          > <tbaudessonENLE VER@harrysoftwa re.ENVELERcom> wrote:
          >[color=green]
          > > Bonjour à Michael Winter <M.Winter@bluey onder.co.invali d> qui nous a
          > > écrit :
          > >[color=darkred]
          > >> On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kkrolski@wi.rr .com> wrote:
          > >>
          > >>> How do I force the text generated by a document.write command to wrap
          > >>> after a specified number of characters?
          > >>
          > >> Insert \n into the string to force a new line. For example:
          > >>
          > >> document.write( 'First line\nSecond line');[/color]
          > >
          > > I prefer the "<br>" tag (Html) : document.write( 'First line<br>Second
          > > line');[/color]
          >
          > What is most appropriate depends on the context. The OP didn't specify.
          >
          > Mike
          >
          > --
          > Michael Winter
          > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


          Comment

          • @SM

            #6
            Re: Document.write



            Michael Winter a ecrit :[color=blue]
            >
            > On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
            > <tbaudessonENLE VER@harrysoftwa re.ENVELERcom> wrote:
            >[color=green]
            > > Bonjour à Michael Winter <M.Winter@bluey onder.co.invali d> qui nous a
            > > écrit :
            > >[color=darkred]
            > >> On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kkrolski@wi.rr .com> wrote:
            > >>
            > >>> How do I force the text generated by a document.write command to wrap
            > >>> after a specified number of characters?
            > >>
            > >> Insert \n into the string to force a new line. For example:
            > >>
            > >> document.write( 'First line\nSecond line');[/color]
            > >
            > > I prefer the "<br>" tag (Html) : document.write( 'First line<br>Second
            > > line');[/color]
            >
            > What is most appropriate depends on the context. The OP didn't specify.[/color]

            and much better (if is send to a popup for instance)

            document.write( 'First line\n<br>Secon d line');

            \n will do a return for JS returns on line in html code
            <br> will do a return for html returns on page displayed in window's browser

            --
            ******** (enlever/remove [OTER_MOI] du/from reply url) *******
            Stéphane MORIAUX : mailto:stephane OTER_MOImoriaux @wanadoo.fr
            Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

            *************** *************** *************** *************** **

            Comment

            • Michael Winter

              #7
              Re: Document.write

              On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kkrolski@wi.rr .com> wrote:
              [color=blue]
              > What do I do if the text to be written is a variable and I have no
              > control of the content?
              >
              > For example
              > ...<tr><td colspan=2 align=left><fon t size="2">' + text +
              > '</font></td></tr>...[/color]

              Here, using <br> in place of \n would be most appropriate. However, I
              would suggest that you avoid breaking lines of text in pages. If the
              internal browser area (canvas, if you will) is smaller than you designed
              for, your page will look horrible.

              |------------|
              You could
              end
              up with text
              like
              this.

              |-------------|
              You won't end
              up with text
              like this.


              Instead, try specifying a width for the table cell (and other containers)
              and let the browser wrap the contents. Do realise that this advice is from
              a general point of view. If you have specific reasons that mean that
              wrapping must be forced and cannot be left to the browser, then go ahead
              with the use of <br>.

              Mike

              --
              Michael Winter
              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

              Comment

              • Ken

                #8
                Re: Document.write

                Unfortunately, I cannot use <br> or /n since I do not have control over the
                text. I do not know where the sentences should break or where there are
                carriage returns.

                I tried setting the table and cell widths, but these did not reduce the
                width of the cell since the text is greater than the cell width. The
                browser increases the cell width to fit the text.

                Do I have any other options? commands?

                This is a difficult problem but others must of faced it. I figure I must be
                missing a command to wrap the text in a document.write statement with a
                variable for the text. I expect this should be done often on webpages.

                Thanks for the continuing help.

                Ken

                "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                news:opr2lh3qku 5vklcq@news-text.blueyonder .co.uk...[color=blue]
                > On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kkrolski@wi.rr .com> wrote:
                >[color=green]
                > > What do I do if the text to be written is a variable and I have no
                > > control of the content?
                > >
                > > For example
                > > ...<tr><td colspan=2 align=left><fon t size="2">' + text +
                > > '</font></td></tr>...[/color]
                >
                > Here, using <br> in place of \n would be most appropriate. However, I
                > would suggest that you avoid breaking lines of text in pages. If the
                > internal browser area (canvas, if you will) is smaller than you designed
                > for, your page will look horrible.
                >
                > |------------|
                > You could
                > end
                > up with text
                > like
                > this.
                >
                > |-------------|
                > You won't end
                > up with text
                > like this.
                >
                >
                > Instead, try specifying a width for the table cell (and other containers)
                > and let the browser wrap the contents. Do realise that this advice is from
                > a general point of view. If you have specific reasons that mean that
                > wrapping must be forced and cannot be left to the browser, then go ahead
                > with the use of <br>.
                >
                > Mike
                >
                > --
                > Michael Winter
                > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


                Comment

                • Dr John Stockton

                  #9
                  Re: Document.write

                  JRS: In article <PWpSb.3426$2h. 2797@twister.rd c-kc.rr.com>, seen in
                  news:comp.lang. javascript, Ken <kkrolski@wi.rr .com> posted at Fri, 30
                  Jan 2004 03:59:33 :-
                  [color=blue]
                  >How do I force the text generated by a document.write command to wrap after
                  >a specified number of characters?[/color]

                  To do what you ask for : construct, if necessary by concatenation, a
                  single string X for document.write( X) ; and, in between, do

                  X = X.replace(/(.{6})/g, "$1<br>")

                  where 6 represents the specified number and <br> is used to wrap (you
                  might want \n). If the number is a computed variable, you'll need to
                  use new RegExp(S1+N+S2) to absorb a string containing the value N.

                  To break at the last word-breaks before the N'th character will be
                  harder.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                  Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                  PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                  Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                  Comment

                  • Peter

                    #10
                    Re: Document.write

                    If I'm understanding correctly, you need to format the length out the
                    context of what the text is.. Why can't you just format the text like you
                    would any other text within a html page. Put the document.write( ) within
                    a <td> and control the with of the <td> or put the document.write( ) within
                    a div and control the with of a div..

                    Pete


                    On 2004-01-30 11:26:50 -0800, "Ken" <kkrolski@wi.rr .com> said:
                    [color=blue]
                    > Unfortunately, I cannot use <br> or /n since I do not have control over[/color]
                    the[color=blue]
                    > text. I do not know where the sentences should break or where there are
                    > carriage returns.
                    >
                    > I tried setting the table and cell widths, but these did not reduce the
                    > width of the cell since the text is greater than the cell width. The
                    > browser increases the cell width to fit the text.
                    >
                    > Do I have any other options? commands?
                    >
                    > This is a difficult problem but others must of faced it. I figure I must[/color]
                    be[color=blue]
                    > missing a command to wrap the text in a document.write statement with a
                    > variable for the text. I expect this should be done often on webpages.
                    >
                    > Thanks for the continuing help.
                    >
                    > Ken
                    >
                    > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                    > news:opr2lh3qku 5vklcq@news-text.blueyonder .co.uk...[color=green]
                    > > On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kkrolski@wi.rr .com> wrote:
                    > >[color=darkred]
                    > > > What do I do if the text to be written is a variable and I have no
                    > > > control of the content?
                    > > >
                    > > > For example
                    > > > ...<tr><td colspan=2 align=left><fon t size="2">' + text +
                    > > > '</font></td></tr>...[/color]
                    > >
                    > > Here, using <br> in place of \n would be most appropriate. However, I
                    > > would suggest that you avoid breaking lines of text in pages. If the
                    > > internal browser area (canvas, if you will) is smaller than you[/color][/color]
                    designed[color=blue][color=green]
                    > > for, your page will look horrible.
                    > >
                    > > |------------|
                    > > You could
                    > > end
                    > > up with text
                    > > like
                    > > this.
                    > >
                    > > |-------------|
                    > > You won't end
                    > > up with text
                    > > like this.
                    > >
                    > >
                    > > Instead, try specifying a width for the table cell (and other[/color][/color]
                    containers)[color=blue][color=green]
                    > > and let the browser wrap the contents. Do realise that this advice is[/color][/color]
                    from[color=blue][color=green]
                    > > a general point of view. If you have specific reasons that mean that
                    > > wrapping must be forced and cannot be left to the browser, then go[/color][/color]
                    ahead[color=blue][color=green]
                    > > with the use of <br>.
                    > >
                    > > Mike
                    > >
                    > > -- > Michael Winter
                    > > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]
                    >
                    >
                    >[/color]


                    Comment

                    • LJL

                      #11
                      Re: Document.write

                      "Ken" <kkrolski@wi.rr .com> wrote in
                      news:PWpSb.3426 $2h.2797@twiste r.rdc-kc.rr.com:
                      [color=blue]
                      > How do I force the text generated by a document.write command to wrap
                      > after a specified number of characters?
                      >
                      > Thanks for the help.
                      >
                      >[/color]

                      You can try this:

                      var the_text = "this is the demo text";
                      var text_pt = the_text.substr ing(5,10);
                      document.write( text_pt);

                      The "5" and "10" (5,10) are the zero-based start and end points of what
                      will be gleaned from the original string.

                      If your original text variable is long, a for loop could be used to grab
                      succeeding sections of the string. You just have to be sure you don't grab
                      anything beyond the length.

                      Anyway, try that out and see if it will owrk for you.

                      Good luck, LJL

                      Comment

                      • Randy Webb

                        #12
                        Re: Document.write

                        LJL wrote:
                        [color=blue]
                        > "Ken" <kkrolski@wi.rr .com> wrote in
                        > news:PWpSb.3426 $2h.2797@twiste r.rdc-kc.rr.com:
                        >
                        >[color=green]
                        >>How do I force the text generated by a document.write command to wrap
                        >>after a specified number of characters?
                        >>
                        >>Thanks for the help.
                        >>
                        >>[/color]
                        >
                        >
                        > You can try this:
                        >
                        > var the_text = "this is the demo text";
                        > var text_pt = the_text.substr ing(5,10);
                        > document.write( text_pt);[/color]

                        And your example will fail on this line:

                        the_text = "Examples compound the problem."

                        Will output this:

                        es co

                        It ignores the rest of the the text.

                        The OP might want to look at:
                        <URL: http://members.aol.com/_ht_a/hikksno...ypingText.html />

                        and see how I did it there (it doesn't break words, it backs up to the
                        last space).
                        [color=blue]
                        > The "5" and "10" (5,10) are the zero-based start and end points of what
                        > will be gleaned from the original string.
                        >
                        > If your original text variable is long, a for loop could be used to grab
                        > succeeding sections of the string. You just have to be sure you don't grab
                        > anything beyond the length.[/color]

                        Expanding your original code, its output would be this:

                        var the_text = "Examples compound the problem."

                        Examp
                        les c
                        ompou
                        nd th
                        e pro
                        blem.

                        I will let you write the code that produces that.

                        --
                        Randy
                        Chance Favors The Prepared Mind
                        comp.lang.javas cript FAQ - http://jibbering.com/faq/

                        Comment

                        Working...