onpaste event (Internet Explorer)

Switch View :
ScriptFree
onpaste event

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

Fires on the target object when the user pastes data, transferring the data from the system clipboard to the document.

Syntax

HTML Attribute <element onpaste = "handler(event)">
Event Property object.onpaste = handler;
attachEvent Method object.attachEvent("onpaste", handler)

Standards information

There are no standards that apply here.

Event information

SynchronousNo
BubblesNo
CancelableNo

Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Remarks

Creating custom code to enable the Paste command requires several steps.

  1. Set the event object returnValue to false in the onbeforepaste event to enable the Paste shortcut menu item.
  2. Cancel the default behavior of the client by setting the event object returnValue to false in the onpaste event handler. This applies only to objects, such as the text box, that have a default behavior defined for them.
  3. Specify a data format in which to paste the selection through the getData method of the clipboardData object.
  4. Invoke the method in the onpaste event to execute custom paste code.

Inserts the data from the system clipboard into the specified location on the document.

To invoke this event, do one of the following:

  • Right-click to display the shortcut menu and select Paste.
  • Or press CTRL+V.

The pEvtObj parameter is required for the following interfaces:

  • HTMLAnchorEvents2
  • HTMLAreaEvents2
  • HTMLButtonElementEvents2
  • HTMLControlElementEvents2
  • HTMLDocumentEvents2
  • HTMLElementEvents2
  • HTMLFormElementEvents2
  • HTMLImgEvents2
  • HTMLFrameSiteEvents2
  • HTMLInputFileElementEvents2
  • HTMLInputImageEvents2
  • HTMLInputTextElementEvents2
  • HTMLLabelEvents2
  • HTMLLinkElementEvents2
  • HTMLMapEvents2
  • HTMLMarqueeElementEvents2
  • HTMLObjectElementEvents2
  • HTMLOptionButtonElementEvents2
  • HTMLScriptEvents2
  • HTMLSelectElementEvents2
  • HTMLStyleElementEvents2
  • HTMLTableEvents2
  • HTMLTextContainerEvents2
  • HTMLWindowEvents2

Examples

This example uses the clipboardData object to implement custom editing functionality.

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


<HEAD>
<SCRIPT>
var sNewString = "new content associated with this object";
var sSave = "";
// Selects the text that is to be cut.
function fnLoad() {
    var r = document.body.createTextRange();
    r.findText(oSource.innerText);
    r.select();
}
// Stores the text of the SPAN in a variable that is set 
// to an empty string in the variable declaration above.
function fnBeforeCut() {
    sSave = oSource.innerText;
    event.returnValue = false;
}
// Associates the variable sNewString with the text being cut.
function fnCut() {
    window.clipboardData.setData("Text", sNewString);
}
function fnBeforePaste() {
    event.returnValue = false;
}
// The second parameter set in getData causes sNewString 
// to be pasted into the text input. Passing no second
// parameter causes the SPAN text to be pasted instead.
function fnPaste() {
    event.returnValue = false;
    oTarget.value = window.clipboardData.getData("Text", sNewString);
}
</SCRIPT>
</HEAD>
<BODY onload="fnLoad()">
<SPAN ID="oSource" 
      onbeforecut="fnBeforeCut()" 
      oncut="fnCut()">Cut this Text</SPAN>
<INPUT ID="oTarget" TYPE="text" VALUE="Paste the Text Here"
      onbeforepaste="fnBeforePaste()" 
      onpaste="fnPaste()">
</BODY>

See also

a
address
applet
area
b
bdo
big
blockQuote
body
button
caption
center
cite
code
custom
dd
dfn
dir
div
dl
dt
em
embed
fieldSet
font
form
hn
hr
i
img
input type=button
input type=checkbox
input type=file
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
kbd
label
legend
li
listing
map
marquee
menu
nextID
noBR
ol
p
plainText
pre
rt
ruby
s
samp
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp
Reference
getData
onbeforecopy
onbeforecut
onbeforepaste
oncopy
oncut
Conceptual
About DHTML Data Transfer

 

 

Build date: 3/14/2012

Community Content

Chris O_Brien
How to customize what is pasted into an element
The "Remarks" numbered lists is wildly inaccurate. To intercept a 'paste' before the clipboard's text data is pasted, simply do this: &nbsp;var textbox = document.getElementById(...); &nbsp;&nbsp;textbox.onpaste = function(e) { &nbsp;&nbsp;&nbsp;this.value = clipboardData.getData('Text').substring(0, 3); &nbsp;&nbsp;&nbsp;return false; &nbsp;}; In this example, you'll catch and cancel all pastes to a &lt;textarea&gt;, instead inserting the first three characters in the clipboard instead of the entire clipboard text.

bigbugx
onpaste event and editable iframe
I can't get onpaste event working for an editable iframe. I tried the same syntax for other events like "onmouseover", it works for iframe but just not onpaste! I can only simulate paste event by capturing "ctrl-v", but if users use right click-> paste, I can't capture it. What is the workaround?

Mr. Raymond Kenneth Petry
Caution - event loss - element interaction

A P-wrapper containing a DIV/DIV misses its onpaste event.

neg. (Edit-Paste into the DIV)

<P id=wrapper onpaste=alert(66)>
<DIV contenteditable>xxx</DIV>
</P>

PS. Also true for standalone-TD-wrapper not in TABLE, but, not-true for DT-wrapper not in DL.


Mr. Raymond Kenneth Petry
Cautionary remark - pasting large files
Edit-Copy-Pasting a large 1MB-text file...

The user will inadvertently Paste a prior clipBoard value until the Copy/Cut data is ready.

N.B. There is -no- known 'clipboardIsReady()' nor 'pasteIsReady()' method.