Monday, March 03, 2003

CodeBit; How Do I Store Text in the Windows Clipboard Using JavaScript?

Click on the first line of text in order to see that text pasted into the third line of text:

<HEAD>
<SCRIPT>
function IsWhitespace(ch) {
  return(
    ch == ' ' || ch == '\n' || ch == '\r' || 
    ch == '\t' || ch == '\f' || ch == '\v' || ch == '\b'
  );
}

// SaveToClibboard
function S2CB() {
  maxLen = 80;
  numLines = event.srcElement.innerText.length / maxLen;
  newStr = '';
  startPos = 0;
  for (x = 0; x < numLines; x++) {
    endPos = startPos + maxLen;
    while (! IsWhitespace(event.srcElement.innerText.charAt(endPos)))
      endPos--;
    newStr = newStr + event.srcElement.innerText.substring(startPos, endPos) + '\n';
    startPos = endPos + 1;
  }
  newStr = newStr + event.srcElement.innerText.substring(
    startPos, event.srcElement.innerText.length
  ) + '\n';
  window.clipboardData.setData('Text', newStr);
  oSource2.innerText = window.clipboardData.getData('Text');
}
</SCRIPT>
</HEAD>
<BODY>
<B ID="oSource" onClick="S2CB()">@changes 2000-Nov-01 
  DMM DMM00013 Silentsurf(nph-index.cgi,Interface.pm): 
  Initial hits to the server are immediately routed to the startup 
  form which eliminates a lot of module loading in the 
  Silentsurf package./</B><br>
<B ID="oSource1" onClick="S2CB()">222</B><br>
<pre>!!<B ID="oSource2" onClick="S2CB()">333</B>!!</pre><br>
</BODY>
</HTML>
Post a Comment