1

I want to handle the paste event after clicking right right and select Paste in IE. As an example i did this:

 parent.document.frames["myframe"].document.attachEvent('onclick', function(e) {
      alert("paste");
});

and it works ok. But when I add 'onpaste' instead of 'onclick' it doesn't work. Also I am using javascript and not jquery.

Does anyone have an idea of how this could work?

Thanks

novellino
  • 1,069
  • 4
  • 22
  • 51
  • FYI, Opera doesn't support `onpaste` but it does support `oninput` (which will fire when the user pastes, among other forms of input). – Andy E Feb 18 '11 at 15:01

3 Answers3

3

You need to attach the event handler to the <body> element rather than the document because the paste event won't bubble up beyond the <body> element in IE. For example:

parent.document.frames["myframe"].document.body.attachEvent('onpaste', function(e) {
    alert("paste");
});
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • @novellino: Do you have any text inputs or `contenteditable` elements in the document? Pastes are only valid within such elements. – Tim Down Dec 07 '10 at 16:45
  • Well I got an error message saying that: 'parent.document.frames.myframe.document.body' is null or is not an object. What does this mean? – novellino Dec 07 '10 at 16:47
  • This body is actually a text editor so maybe it has not a contenteditable element. In Firefox I used $(window).bind('paste', function(e){}); and it worked ok. Why I can not use this bind command in IE as well? – novellino Dec 07 '10 at 16:53
  • @novellino: That means you're trying to attach the event before frame's body exists. I would suggest putting the code that attaches the event into a function which you then call from the frame once the frame's document has loaded. If you can't change the frame's document, you could instead poll the frame to check if the body exists and attach the event handler once it does. – Tim Down Dec 07 '10 at 16:55
  • The reason why the same command doesn't work in IE is as I stated in may answer: `paste` events don't bubble beyond the `` element in IE. Specifically, this means a `paste` event never reaches the `window` object. – Tim Down Dec 07 '10 at 16:57
  • Thank you very much, I will try to add them in my code and see how it will go. – novellino Dec 07 '10 at 17:25
  • I still haven't managed to do this. Do you have any code sample to show me how to do it? – novellino Dec 08 '10 at 15:28
  • I need also to add that I have everything in a load() function, which called in onload; – novellino Dec 08 '10 at 16:14
2

@novellino seeing as i dont have enought rep to leave a comment on Tims answer, ill have to do it here. This is using jQuery (reading your comment looks like youre using it anyway):

$("#iframeid").contents().find("body").bind('paste', function() {
  // Your code here...
});

This works in both IE and FF, oh and chrome.

I too was using "$(window).bind('paste', function(e){})" But as Tim says:

"paste event won't bubble up beyond the <body> element in IE"

Therfore try the code i attached, that worked for me.

Hope that helps... Al

Community
  • 1
  • 1
Alex
  • 8,875
  • 2
  • 27
  • 44
0

You might want to check this demo on quirskmode.org on how to do this.

Shawn Steward
  • 6,773
  • 3
  • 24
  • 45