1

I have been trying for few hours to get the data copied in the clipboard in chrome its working in IE chrome gives me an error the sample code follows

window.clipboardData.getData("Text")
Mathew Paul
  • 647
  • 3
  • 16
  • 36
  • Possible Duplicate: http://stackoverflow.com/questions/127040/copy-put-text-on-the-clipboard-with-firefox-safari-and-chrome –  Aug 12 '11 at 05:16

6 Answers6

4

Chrome doesn't allow access to the clipboard.

It doesn't even have a window.clipboardData object.

A workaround is to possibly use a hidden Flash movie.

alex
  • 479,566
  • 201
  • 878
  • 984
  • What my requirement is "I have copied some content from a document and want to paste the content in a text area of my application so i need to check the maxlength of that textarea and paste accordingly" – Mathew Paul Aug 12 '11 at 05:16
  • @mathew Why don't you use a textbox instead? or put an on change that removes the extra text with a message that the user has exceeded the maxlength? – James Khoury Aug 12 '11 at 05:26
  • @James Khoury same thing happens for text box with property set as multiline – Mathew Paul Aug 12 '11 at 06:18
  • @Mathew You don't need to on a text input as it has a size attribute you can set. – James Khoury Aug 12 '11 at 06:19
1

You can retrieve clipboardData from event in your function. In your "paste" event handler (pure Javascript) use event.clipboardData or event.originalEvent.clipboardData in JQuery event handler

rock_walker
  • 453
  • 5
  • 14
1

Chrome does give you access to the paste event and you can create a class naming structure to take advantage. Here i am updating a RadMaskedTextBox to use a css class on the parent span and then use the following code where "NumericBlur" is my input box's parent wrappercssclass.

<telerik:RadMaskedTextBox ID="SSN" runat="server" Width="100" RequireCompleteText="false" WrapperCssClass="NumericBlur rdfInput"  Mask="###-##-####"></telerik:RadMaskedTextBox>


 window.addEventListener('paste', function (event) {

    var data = event.clipboardData.getData('text');

    if (event.srcElement.parentElement.className.contains("NumericBlur")) {

        event.srcElement.value=data.replace(/\D/g, "");
    }
});

'

zarkara
  • 11
  • 1
0

Don't know if this issue is still live but it is possible to read the windows clipboard using the Chrome: document.execCommand('paste') command.

Steve
  • 1
0

For those who are still in search, this snippet could be useful:

window.addEventListener('paste', function(event){
    event.preventDefault();

    var data = event.clipboardData.getData('text');

    alert(data);
});
tenbits
  • 7,568
  • 5
  • 34
  • 53
  • Combining both rodk_walker and tenbits answers, it works for chrome and firefox "event.originalEvent.clipboardData.getData('text')". At least for now (march 2015). Thank you both. – Alejandro Illecas Mar 03 '15 at 18:49
0

Browsers these days don't allow that. You will have to go with a hidden flash object to be able to copy to the clipboard.

Here is a flash copy to clipboard system, zClip. You could also try ZerioClipboard.

Most websites do this if they want something to be copied to the clipboard. Bit.ly uses flash to copy the link. See:

enter image description here

Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
Nathan
  • 11,814
  • 11
  • 50
  • 93