Forum Moderators: open

Message Too Old, No Replies

Text as table detail

         

manjumurthy

9:55 am on Sep 13, 2001 (gmt 0)



Hi all,
Can anyone give me a script function that allows only 10 characters per line. The entire text is a detail in the table.
Note: I want to perform a function similar to wordwrap because the default propert of wrap fails when the text is a single word.
Thanks
manjumurthy

joshie76

12:59 pm on Sep 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<script>
<!--
//Split by Josh Twist 13/09/2001
// splitlen - length of each line
//text - text to be split
function split(splitlen, text)
{
var sOutput;
var sSplitText = new Array();
var iStartPos = 0;
var iCurrPos = 0;
var sOutput = '';

while (iStartPos < text.length)
{
sSplitText[iCurrPos] = text.substring(iStartPos,iStartPos + splitlen);
iStartPos = iStartPos + splitlen;
iCurrPos++
}

for (var p=0; p <= sSplitText.length; p++)
{
sOutput += sSplitText[p] + "<br>";
}

document.write (sOutput);
}

//text to be split as an example
var sText = "Refined and enhanced over the last 15 years, with input from hundreds of major organisations at the forefront of human resource management, and quite simply the world's leading system for leadership development, succession planning and competency management."

//call the function
split(10,sText)

//-->
</script>

Does that do the trick?

[edited by: tedster at 3:25 pm (utc) on Aug. 14, 2003]

joshie76

1:51 pm on Sep 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK - got it (I think) , this is better...

wraps at spaces instead of mid word where possible. Inserts hyphens when breaking words.

<script>
<!--
//Split by Josh Twist 13/09/2001
// splitlen - length of each line
//text - text to be split

// define punctuation chars to not break before...
var sPunctChars = new Array();
sPunctChars[0] = "?";
sPunctChars= "!";
sPunctChars[2] = ".";
sPunctChars[3] = ",";
sPunctChars[4] = ";";
sPunctChars[5] = "-";

function split(splitlen, text)
{
var sOutput;
var sSplitText = new Array();
var iStartPos = 0;
var iCurrPos = 0;
var sOutput = '';
var iSpaceIndex;
var iGramSpacing = 0;

while (iStartPos < text.length)
{
iGramSpacing = 0;
iSpaceIndex = text.substring(iStartPos,iStartPos + splitlen).lastIndexOf(" ");

if (iSpaceIndex!= -1 && iSpaceIndex!= 0)
{
sSplitText[iCurrPos] = text.substring(iStartPos,iStartPos + iSpaceIndex + iGramSpacing);
iStartPos = iStartPos + iSpaceIndex + iGramSpacing;
iCurrPos++
}
else if (iSpaceIndex == 0)
{
iStartPos = iStartPos + 1
}
else
{
for (var i=0; i < sPunctChars.length; i++)
{
if (text.charAt(iStartPos + splitlen) == sPunctChars[i] )
{
iGramSpacing = 1;
}
}

sSplitText[iCurrPos] = text.substring(iStartPos,iStartPos + splitlen + iGramSpacing);
if (iGramSpacing == 0 &&
text.charAt(iStartPos + splitlen)!= " " &&
iStartPos + splitlen < text.length
)
{
sSplitText[iCurrPos] += "-"
}
iStartPos = iStartPos + splitlen + iGramSpacing;
iCurrPos++
}
}

for (var p=0; p < sSplitText.length; p++)
{
sOutput += sSplitText[p] + "<br>";
}

document.write (sOutput);
}

var sText = "Refined and enhanced over the last 15 years, with input from hundreds of major organisations at the forefront of human resource management, and quite simply the world's leading system for leadership development, succession planning and competency management."

split(20,sText)

//-->
</script>

[1][edited by: tedster at 3:26 pm (utc) on Aug. 14, 2003]