Forum Moderators: open
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]
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... function split(splitlen, text) while (iStartPos < text.length) if (iSpaceIndex!= -1 && iSpaceIndex!= 0) sSplitText[iCurrPos] = text.substring(iStartPos,iStartPos + splitlen + iGramSpacing); for (var p=0; p < sSplitText.length; p++) 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) //--> [1][edited by: tedster at 3:26 pm (utc) on Aug. 14, 2003]
var sPunctChars = new Array();
sPunctChars[0] = "?";
sPunctChars= "!";
sPunctChars[2] = ".";
sPunctChars[3] = ",";
sPunctChars[4] = ";";
sPunctChars[5] = "-";
{
var sOutput;
var sSplitText = new Array();
var iStartPos = 0;
var iCurrPos = 0;
var sOutput = '';
var iSpaceIndex;
var iGramSpacing = 0;
{
iGramSpacing = 0;
iSpaceIndex = text.substring(iStartPos,iStartPos + splitlen).lastIndexOf(" ");
{
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;
}
}
if (iGramSpacing == 0 &&
text.charAt(iStartPos + splitlen)!= " " &&
iStartPos + splitlen < text.length
)
{
sSplitText[iCurrPos] += "-"
}
iStartPos = iStartPos + splitlen + iGramSpacing;
iCurrPos++
}
}
{
sOutput += sSplitText[p] + "<br>";
}
}
</script>