In 2017 you can do this (saying this because this thread is almost 9 years old!)
function copyStringToClipboard (string) {
function handler (event){
event.clipboardData.setData('text/plain', string);
event.preventDefault();
document.removeEventListener('copy', handler, true);
}
document.addEventListener('copy', handler, true);
document.execCommand('copy');
}
And now to copy copyStringToClipboard('Hello World')
If you noticed the setData
line, and wondered if you can set different data types the answer is yes.