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;
}
The window.clipboardData.getData("Text")
doesn't work in Chrome browser
Is there any crossbrowser code to substitute it?
Also when user presses a shortcut like F9, we need to do the same. As chrome have restriction in reading clipboard data, we can read data from clipboard only in paste events. In my case I don't have paste event and also can't manually trigger the paste event in chrome.
In Chrome, for instance, you can get the clipboardData when handling the paste events [ ^ ], But, you must consider asking the user to manually copy/paste the contents. That is a more cross-browser way of doing this job for them.
Please Sign up or sign in to vote. Please Sign up or sign in to vote. Because that is only supported by IE itself, it is not supported cross-browser. In Chrome, for instance, you can get the clipboardData when handling the paste events [ ^ ],
No, there is no cross-browser support for window.clipboardData
. It is only supported by IE. Support for window.clipboardData
is generally considered a security issue because it allows every website you visit to read whatever happens to be in your clipboard at the time.
In Chrome, you can read clipboardData
when handling paste events:
document.addEventListener('paste', function (evt) {
console.log(evt.clipboardData.getData('text/plain'));
});
Cross browser method should be
document.addEventListener('paste', function (evt) {
clipdata = evt.clipboardData || window.clipboardData;
console.log(clipdata.getData('text/plain'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With