From http://www.w3.org/TR/clipboard-apis/ 5.1.3 insert event
The insert event has no default action in an uneditable context, but the event fires independently.
Chrome uses a lazy insert event, which means that it does not check if the focused element is an editable content area, so the document uses the paste event.
As for firefox and IE, it actually checks the element before allowing the paste event to fire. So basically you need an editing element for the content so that the insert event is used in all browsers.
I played using DIV-editable content as the main div on the page, and I think it seems to give the results you are looking for. Forgive me if the answer seems somewhat "hacky."
You can place the contenteditable div as a container div on the rest of your web page and not allow the user to type in the div, returning false when the user presses a key, unless it is an insert key.
Here is the fiddle if you want to see how I did it.
http://jsfiddle.net/NVqQ7/3/
HTML
<div id="pageDiv" contenteditable><div>other content</div></div>
CSS
html, body, #pageDiv { width: 100%; height: 100%; margin: 0; padding: 0; }
JavaScript:
$(document).on('paste', function(event){ var oEvent = event.originalEvent; if (event.preventDefault()) event.preventDefault(); var clipText = ''; if(window.clipboardData){ clipText = window.clipboardData.getData('Text'); }else if(typeof oEvent == 'object' && oEvent.clipboardData){ clipText = oEvent.clipboardData.getData('text/plain'); } // console.log('Pasted ' + clipText.length + ' characters.'); alert('Pasted ' + clipText.length + ' characters.'); }); $('#pageDiv').keydown(function(e){ if (!event.ctrlKey) return false; });
A few notes:
The user must click on the text of the page to activate the insert event. that is, clicking on the browser alone may not be sufficient.
In firfox, you need to set contenteditable = "false" in the child divs, otherwise users can add text to these elements.
I needed to make the height and width of the div 100%. If your html and body are not 100% height and width, it will not work in IE.
I had to change the jquery library to a newer version for IE for the paste event to work in it.
Hope this helps
source share