7

Can you tell me how can I just get text who is copied in clipboard. I don't want to make a copy because data are copied from Excel. In IE I use :

  var clipText = window.clipboardData.getData('Text');

And it's work perfect. Is it possible in chrome ? or maybe Firefox ?

Thanks for advance

1

2 Answers 2

17

The window.clipboardData object is only available in IE. It seems like a big security vulnerability to me for a website to be able to access your clipboard data, especially without you knowing. According to the specification, it's mostly deprecated as of Microsoft Edge.

Instead, you can access the data by listening to the paste event:

document.addEventListener('paste', function (event) {
  var clipText = event.clipboardData.getData('Text');
});
3
  • 2
    Ok so users are forced to make ctrl+V on Navigator for that I can get data ?
    – clementine
    Commented Jul 18, 2016 at 10:15
  • 1
    @clementine Yes, so that your app isn't allowed to steal potentially sensitive information from the end-user's clipboard without them knowing. Commented Jul 18, 2016 at 11:27
  • In other browsers, you can use the Clipboard API to read from the clipboard. Commented Aug 30, 2021 at 21:45
5

If you're looking to use jQuery and bind an element to the 'paste' event then you can access the clipboard data by using the originalEvent property on the calling event.

Check the window object to see if the clipboardData is undefined. This will mean that you're not using IE or Edge.

this.bind('paste', function(e){
if (window.clipboardData === undefined)
    clipText = e.originalEvent.clipboardData.getData('Text') // use this method in Chrome to get clipboard data.
else
    clipText = window.clipboardData.getData('Text') // use this method in IE/Edge to get clipboard data.
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.