18 out of 26 rated this helpful - Rate this topic

clipboardData object

[This documentation is preliminary and is subject to change.]

Provides access to predefined clipboard formats for use in editing operations.

Standards information

There are no standards that apply here.

Remarks

The clipboardData object is reserved for editing actions performed through the Edit menu, shortcut menus, and shortcut keys. It transfers information using the system clipboard, and retains it until data from the next editing operation replaces it. This form of data transfer is particularly suited to multiple pastes of the same data.

This object is available in script as of Microsoft Internet Explorer 5.

Examples

This example uses the setData and getData methods with the clipboardData object to perform a cut-and-paste operation through the shortcut menu.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/clipboardDataEX.htm


<HTML>
<HEAD>
<SCRIPT>
var bResult;
// Select the text to be cut. Trailing spaces in a text
// selection in cut events cause the Cut shortcut menu item to
// remain disabled.
function fnLoad() {
    var r = document.body.createTextRange();
    r.findText(oSource.innerText);
    r.select();
}
// Enable the Cut shortcut menu item over the DIV. Cut is disabled by default.
// Once Cut is enabled, Internet Explorer automatically copies the data to the
// clipboard and removes the selected text from the document.
function fnBeforeCut() {
    event.returnValue = false;
}
//Assign data in text format to the window.clipboardData object.
//Display the result (Boolean) from the setData method in the input box below.
function fnCut(){
	event.returnValue = false;
	bResult = window.clipboardData.setData("Text",oSource.innerText);
	oSource.innerText = "";
	tText.innerText += bResult;
}
// Enable the Paste shortcut menu item over the DIV. Paste is disabled by default.
function fnBeforePaste() {
    event.returnValue = false;
}
// Cancel the returnValue in onpaste for the text input, which
// has a default behavior.
function fnPaste() {
    event.returnValue = false;
	oTarget.innerText = window.clipboardData.getData("Text");
}
</SCRIPT>
</HEAD>
<BODY onload="fnLoad()" TOPMARGIN=0 LEFTMARGIN=0 BGPROPERTIES="fixed" BGCOLOR="#FFFFFF"
	LINK="#000000" VLINK="#808080" ALINK="#000000">
<DIV CLASS="clsSource" ID="oSource" onbeforecut="fnBeforeCut()" oncut="fnCut()">
	Select and cut this text
</DIV>
<DIV CLASS="clsTarget" ID="oTarget" onbeforepaste="fnBeforePaste()" onpaste="fnPaste()">
	Paste the Text Here
</DIV><BR>
<SPAN CLASS="clsData">setData Result: </SPAN>
<INPUT CLASS="clsText" ID="tText" TYPE="text" READONLY VALUE="" SIZE="6" TABINDEX="-1">
</BODY>
</HTML>

See also

Data Transfer Overview

 

 

Build date: 2/14/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SECURITY ANOMALY - PUBLIC USERS
N.B. The clipboard may expose Edit:Copy-Paste and other data of one User, across user-sessions, to the next User: The clipboard does not automatically clear between user-sessions, nor manually clear by the Tools :Delete Browsing History. (IE8; Windows 7)
Limitations...

Underlying HTML is inaccessible in the getData method: contenteditable converts newlines to trivial <BR>|<P></P>...

(This is why onpaste=...getData... is 20x faster than the browser-menu-Edit-Paste 1 min./MB.)

(N.B. The execCommand("paste") method does access the underlying HTML, but also invokes the onpaste event; whereas the pasteHTML(...) method does-not invoke.)

Source context is inaccessible in the getData method: It doesn't know whether the source is the same document, or external.

(PS. Also note that MSIE 7 becomes reluctant at 1.5MB of DHTML element data, failing drag-selection of text, drag-and-drop move-text,...)

Caution - unknown readystate

There's no apparent documentation on the clipBoard readyState:--

Tell your Web-Users to "WATCH FOR LATE COPY-DATA IF THE CLIPBOARD IS BIG...".

But, There's no apparent documentation for a clipBoard onready event...