admin 管理员组

文章数量: 1086019

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")

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")
Share Improve this question asked Aug 12, 2011 at 5:10 Mathew PaulMathew Paul 6634 gold badges16 silver badges37 bronze badges 1
  • Possible Duplicate: stackoverflow./questions/127040/… – user529649 Commented Aug 12, 2011 at 5:16
Add a ment  | 

6 Answers 6

Reset to default 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.

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

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, "");
    }
});

'

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:

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

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);
});

本文标签: javascriptclipboard issue in ChromeStack Overflow