cross browser copy to clipboard

Copy to clipboard via flash >= v9 and javascript.

Please note: This only works for flash v9 and below as it uses action script 2!

This script has been tested on Firefox 2 & 3, Safari 2 & 3, Chrome, Opera & IE 6,7 & 8.

The Demo:

HTML:

<textarea id="copyMe" cols="50" rows="10" name="copyMe"></textarea>
<input id="copyBtn" name="copyBtn" type="button" value="Copy To Clipboard" />

Javascript:

window.addEvent('domready', function() {
 
	function sendtoclipboard(s,el)	{
		if( window.clipboardData && clipboardData.setData )	{
			clipboardData.setData("text", s);
		} else {
			ffcopy(el);
		}
	}
 
	function ffcopy(inElement) {
	  if (inElement.createTextRange) {
		var range = inElement.createTextRange();
		if (range && BodyLoaded==1)
		  range.execCommand('Copy');
	  } else {
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
		  var divholder = document.createElement('div');
		  divholder.id = flashcopier;
		  document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '&lt;embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"&gt;&lt;/embed&gt;';
		document.getElementById(flashcopier).innerHTML = divinfo;
	  }
	}	
 
	$('copyBtn').addEvent('click',function() {
		var el = $('copyMe');
		el.select();
		var t = el.getText();
		sendtoclipboard(t,el);
	});
 
});