I tried the following code.Both onpaste() and Oncopy() will trigger when user trying to copy or paste.
But my problem is while copying something window.getSelection() not working.
Window.getSelection() is used to get the selected data.
I mean before copying something user need to select some values.So,using this window.getselection() we will get the value what they are selected.
Here, my problem is window.getselection() will work in oncopy but not working in onpaste.
Check the below examples and help me to solve the problem.
Also ,is there any alternative method also please let me know.I also tried window.clipboardData.But not getting the solution for this.
<!doctype html>
<html>
<head>
<style>
textarea {
width:500px;
height:200px;
border:2px solid #999;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function OnPaste () {
var selection = window.getSelection();
alert(selection.toString());
}
function OnCopy() {
var selection = window.getSelection();
alert(selection.toString());
}
// Call the OnCopy function while user trying to copy
document.oncopy = OnCopy;
// Call the OnPaste function while user trying to Paste
document.onpaste = OnPaste;
</script>
</head>
<body>
Select some text, copy it to the clipboard and try to paste it to the following fields:
<br /><br />
<textarea size="40" value="Copy and Paste operation is enabled">