6

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

clementine
  • 73
  • 1
  • 2
  • 5
  • Possible duplicate of [How to paste on click? It works in google docs](http://stackoverflow.com/questions/34470272/how-to-paste-on-click-it-works-in-google-docs) – GSerg Apr 27 '17 at 08:41

2 Answers2

16

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');
});
Gideon Pyzer
  • 22,610
  • 7
  • 62
  • 68
  • 2
    Ok so users are forced to make ctrl+V on Navigator for that I can get data ? – clementine Jul 18 '16 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. – Gideon Pyzer Jul 18 '16 at 11:27
  • In other browsers, you can use the [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) to read from the clipboard. – Anderson Green Aug 30 '21 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.
});
Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54
logan gilley
  • 191
  • 2
  • 5