How to add onpaste event in frame in IE with javascript?
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', funct开发者_如何学运维ion(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
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");
});
@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
You might want to check this demo on quirskmode.org on how to do this.
精彩评论