Forum Moderators: open

Message Too Old, No Replies

Testing support for event.clipboardData

         

csdude55

2:38 am on Aug 9, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you guys suggest the "proper" way to test for support for event.clipboardData?

After a whole lot of poking around, I've realized that window.clipboardData() is triggered in $.on('beforepaste'), while event.clipboardData is triggered in $.on('paste'). So this is where I've gotten:

var pasted = false;

$('#foo')
// IE9
.on('beforepaste', function(e) {
if (window.clipboardData) {
// blah blah blah

pasted = true;
return;
}
})

// Chrome and FF
.on('paste', function(e) {
var clp = (e || e.originalEvent).clipboardData;

// this isn't being triggered in Chrome... why?
if (clp && !pasted) {
// blah blah blah

pasted = true;
return;
}

else if (!pasted) {
// blah blah blah
}
})


But it looks like clp is being returned as undefined. Specifically, I get:

clp -- undefined
e.clipboardData -- undefined
e.originalEvent.clipboardData -- [object DataTransfer]

What am I doing wrong here?

csdude55

12:42 am on Aug 14, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For the future, I'm pretty sure that e.originalEvent.clipboardData will always apply to jQuery users, so if I'm using jQuery then I don't need to test for e.clipboardData.

So I ended up with:

.on('paste', function(e) {
var clp;
if (e.originalEvent.clipboardData)
clp = e.originalEvent.clipboardData;

if (clp) {
e.preventDefault ? e.preventDefault() : e.returnValue = false;

var type = 'plain';
if (clp.types.indexOf('text/html') !== -1) type = 'html';

a = editClip(clp.getData('text/' + type));
document.execCommand('insertHTML', false, a);

return;
}

else {
a = editClip($(this).html(), 'off');
$(this).html(a);
}
})