Saturday, January 10, 2009

Copy to Clipboard SWF button (cross browser compatible)

Since Flash 10, there have been security enhancements that prevent data being copied to the clipboard without an event being initiated by the user.
See: http://blogs.pcmag.com/securitywatch/2008/09/adobe_to_plug_clipboardjacking.php and http://www.adobe.com/devnet/flashplayer/articles/fplayer10_uia_requirements.html).
This means that Jeffothy’s Keyings Clipboard Copy trick no longer works :-(

NEW DEVELOPMENT: jhuckaby provided a better solution. Check it out:
Zero Clipboard



I've developed a simple new way to get the job done.
My method uses a basic flash button that says "COPY" on it. Clicking it will copy to the clipboard any text provided via FlashVars parameters in the object/embed tags. The flash file then executes javascript as a callback function. This javascript is also provided via FlashVars.



Actionscript 2.0 Code

on (release) {
System.setClipboard(_level0.txtToCopy);
getURL('javascript:'+_level0.js+';');
}



HTML/JS Code

<script type="text/javascript">
<!--
function alertUser(){
alert('copied');
}
-->
</script>
<div style="border:solid 1px black; width:60px; height:20px;">
<object width="60" height="20">
<PARAM NAME=FlashVars VALUE="txtToCopy=cfRuss.blogspot.com&js=alertUser()">
<param name="movie" value="copyButton.swf">
<embed src="copyButton.swf" flashvars="txtToCopy=cfRruss.blogspot.com&js=alertUser()" width="60" height="20">
</embed>
</object>
</div>


Download the required flash file here: copyButton.swf

FlashVars
txtToCopy - the text that you wish to copy to the clipboard
js - javascript to be executed upon clicking the COPY button

Note
You may get a warning dialog from your flash player if you try to run this script locally. This dialog will not appear when you run the script on your server.