Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'beforepaste' event is not fired in webkit?

beforecopy event is fired, but beforepaste event isn't fired. Why is that?

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type="text/css">#editor{width:300px; height:300px; border: 1px solid black;}</style>
</head>
<body>
<div id="editor" contentEditable="true">editor</div>
<script type="text/javascript">
var elEditor = document.getElementById("editor");

elEditor.addEventListener('beforecopy', function(e){
    console.log('beforecopy');
    e.preventDefault();
    e.stopPropagation();
}); 

elEditor.addEventListener('copy', function(e){
    console.log('copy');
}); 

elEditor.addEventListener('beforepaste', function(e){
    console.log('beforepaste');
    e.preventDefault();
    e.stopPropagation();
}); 

elEditor.addEventListener('paste', function(e){
    console.log('paste')
});
</script>
</body>
</html>
  • Using the Pasteboard From JavaScript
  • fiddle link : http://jsfiddle.net/BjR9q/
like image 884
Hacker Wins Avatar asked Feb 18 '23 06:02

Hacker Wins


1 Answers

On Chrome 23.0.1271.95 on OS X 10.8.2, onbeforepaste only triggers when the user causes a context menu to pop up.

http://help.dottoro.com/ljxqbxkf.php:

Occurs before the contents of the clipboard are pasted into the document and provides a possibility to enable the Paste menu item.

Right-clicking the text-box causes the onbeforepaste event to fire, but not pressing ctrlv.

If it's any consolation, the onpaste event fires before the text is actually pasted, in my tests, so I would just use the onpaste event instead.

like image 85
Waleed Khan Avatar answered Feb 21 '23 02:02

Waleed Khan