Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <body>
- <pre>1---------10--------20--------30--------40--------50--------</pre>
- </body>
- <script>
- var text = "NICHOLAS PEROT SMITH WELCH & SMITH and SUZIE MCFAIRLAIN and ROBERT MCFAIRLAIN MRS. LOREM IPSUM and WORSHESTERSHIRE and OBNOXIOUS FLAVANOIDS and MARRY HAD A LITTLE LAMB AND IT SQUEAKED TWICE";
- var body = document.getElementsByTagName("body")[0];
- var child = document.createElement("pre");
- child.innerHTML = wordWrap(text, 60, 3)
- body.appendChild(child);
- function wordWrap(sentence, cols, rows) {
- var words = sentence.split(/\s+/);
- var lines = [];
- var currentword = 0;
- var space = " ";
- for (var i = 0; i < rows; i++) {
- lines[i] = "";
- while(currentword < words.length) {
- if ((lines[i] + space + words[currentword]).length <= cols) {
- lines[i] += (!lines[i].length ? "" : space) + words[currentword++];
- } else {
- break;
- }
- }
- }
- if (currentword < words.length) {
- var missingwords = "";
- while(currentword < words.length) {
- missingwords += words[currentword++];
- }
- alert("TOO LONG! MISSING:" + missingwords);
- }
- return lines.join("\r\n");
- }
- </script>
- </html>
Add Comment
Please, Sign In to add comment