Window.clipboardData.getData ("Text") does not work in chrome

I have this javascript function:

function maxLengthPaste(field,maxChars)
{
    event.returnValue=false;
    if((field.value.length + window.clipboardData.getData("Text").length) > maxChars) {
        field.value = field.value + window.clipboardData.getData("Text").substring(0, maxChars - field.value.length);
        return false;
    }
    event.returnValue=true;
}

window.clipboardData.getData("Text") not working in Chrome browser Is there any cross-browser code to replace it?

+4
source share
2 answers

No, there is no cross browser support for window.clipboardData. It is supported only by IE. Support is window.clipboardDatagenerally considered a security issue because it allows every website you visit to read everything that happens on your clipboard at that time.

In Chrome, you can read clipboardDatawhen handling insert events:

document.addEventListener('paste', function (evt) {
  console.log(evt.clipboardData.getData('text/plain'));
});
+11
source

Cross browser method should be

document.addEventListener('paste', function (evt) {
  clipdata = evt.clipboardData || window.clipboardData;
  console.log(clipdata.getData('text/plain'));
});
+3

All Articles