views:

77

answers:

2

If I write to long text for example this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

The text is going out of the page, I have an idea to fix it, after every 100 characters I could make a <br /> tag. But I don't know how to do it.

Thanks for any help!

+6  A: 

Just use the following CSS property on the element that you wish to force wrapping in:

word-wrap: break-word;

No need for any JavaScript!

If you really want to use JavaScript (for concept; CSS is better for accessibility and ease) then go ahead.

Oh, and another thing, if you're using a font that isn't monospace (but rather, proportional), cutting off at 100 characters could be ineffective. One line could have 100 'i' characters, with the next having another hundred 'm' chars, which are very different in size.

Oh, and another thing, you can't just apply a regex replace on the innerHTML unless it's all text. If there could possibly be other elements there, you must actually loop through the nodes, applying the technique to only text nodes.

Oh, and another thing, don't bother. Too many problems doing it with javascript.

Delan Azabani
+1  A: 

You can try the following code

  str = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

len = str.length; loop = len/ 100; document.write(loop); document.write('
'); for(i=0;i<=loop;i++){ document.write(str.slice(i*100,(i*100)+100)) document.write('
');

}

jimy