468,720 Members | 1,591 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Document.write

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

Thanks for the help.
Jul 20 '05 #1
11 2418
On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kk******@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');

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Bonjour à Michael Winter <M.******@blueyonder.co.invalid> qui nous a
écrit :
On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kk******@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');

Mike


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

--
Cordialement, Thierry ;-)

Jul 20 '05 #3
On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
<tb***************@harrysoftware.ENVELERcom> wrote:
Bonjour à Michael Winter <M.******@blueyonder.co.invalid> qui nous a
écrit :
On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kk******@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');


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


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

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
Ken
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><font size="2">' + text +
'</font></td></tr>...

Ken

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
<tb***************@harrysoftware.ENVELERcom> wrote:
Bonjour à Michael Winter <M.******@blueyonder.co.invalid> qui nous a
écrit :
On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kk******@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');


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


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

Mike

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

Jul 20 '05 #5
@SM


Michael Winter a ecrit :

On Fri, 30 Jan 2004 13:03:51 +0100, Cenekemoi
<tb***************@harrysoftware.ENVELERcom> wrote:
Bonjour à Michael Winter <M.******@blueyonder.co.invalid> qui nous a
écrit :
On Fri, 30 Jan 2004 03:59:33 -0600, Ken <kk******@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');


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


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


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

document.write('First line\n<br>Second 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:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephane.moriaux/internet/
************************************************** ************
Jul 20 '05 #6
On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kk******@wi.rr.com> wrote:
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><font size="2">' + text +
'</font></td></tr>...


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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #7
Ken
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.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kk******@wi.rr.com> wrote:
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><font size="2">' + text +
'</font></td></tr>...


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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 20 '05 #8
JRS: In article <PW****************@twister.rdc-kc.rr.com>, seen in
news:comp.lang.javascript, Ken <kk******@wi.rr.com> posted at Fri, 30
Jan 2004 03:59:33 :-
How do I force the text generated by a document.write command to wrap after
a specified number of characters?


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.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #9
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" <kk******@wi.rr.com> said:
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.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Fri, 30 Jan 2004 09:22:36 -0600, Ken <kk******@wi.rr.com> wrote:
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><font size="2">' + text +
'</font></td></tr>...


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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


Jul 20 '05 #10
LJL
"Ken" <kk******@wi.rr.com> wrote in
news:PW****************@twister.rdc-kc.rr.com:
How do I force the text generated by a document.write command to wrap
after a specified number of characters?

Thanks for the help.


You can try this:

var the_text = "this is the demo text";
var text_pt = the_text.substring(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
Jul 20 '05 #11
LJL wrote:
"Ken" <kk******@wi.rr.com> wrote in
news:PW****************@twister.rdc-kc.rr.com:

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

Thanks for the help.

You can try this:

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


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


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

Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brett Baisley | last post: by
2 posts views Thread by bissatch | last post: by
11 posts views Thread by Tony | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.