Cross Browser Copy to Clipboard

The only way to copy to clipboard, on all browsers, is to use Flash (IE can do it in JS), but since Flash 10, there have been security enhancements that prevent data being copied to the clipboard without an event being initiated by the user.

A simple method uses a basic flash button which when clicked will copy any text in a variable to the clipboard.

on (release) {
System.setClipboard(_level0.txtToCopy);
}

This can be combined with some JQuery and swfObject to get text from the current webpage. In this example the swf attaches itself to every blockquote and clicking it will copy the visible text of the blockquote.

$(document).ready(function(){

$('blockquote').each(function() {
$(this).flash({
swf: '/wp-content/themes/unchi/copyButton.swf',
height: 16,
width: 92,
params: {
wmode: 'transparent'
},
flashvars: {
txtToCopy: $(this).text()
}
});
});

});

About Keiron

Web Developer based in the UK. Click here if you want to work with me