I was ask for one project to copy data from one of the sites to the user's clipboard, after some research around, I found the "Zero Clipboard" code.
This code works really good, but it is too complicated for my needs.
It requires to much javascript, and this can get you in troubles if you need to run this code inside a popup window, or other actual dynamic HTML elements.

So I've made some changes, and create a much more simple version. which uses a basic flash button with an icon in it.

Clicking it will copy the selected property for a HTML object to the clipboard.
The flash file then executes javascript as a callback function to allow the system to notify the copy proccess.

Basicly you add a Flash button into your HTML with the next parameters:

  • id - the HTML id tag of the element you that you want to copy to the clipboard
  • prop - the property to get from the HTML object (innerHTML,value,...)
  • recall - javascript to be executed upon clicking the copy button

Sending an HTML id, and a property, allows easy access to HTML data. If your object is an image, asking for the property "src" you will get you the path, for an input text , the "value" will give you the copy in the input, and for normal elements like divs,p or span, "innerHTML" will give you the HTML copy.

For example, a SWF loaded with the next parameters:

'copy2clipboard.swf?id=text1&prop=value&recall=afterCopy'

will copy to the clipboard the value of the object with HTML id "text1", after click over the flash component, and then call the js function "afterCopy".

The Flash code to do this is very simple:

Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, text2clipboard);

Since Flash 10, there have been security enhancements that prevent data being copied to the clipboard without an event being initiated by the user.

"In Flash Player 10 applications, a paste operation from the clipboard first requires a user event (such as a keyboard shortcut for the Paste command or a mouse click on the Paste command in a context menu).

Clipboard.getData() will return the contents of the clipboard only if the InteractiveObject has received and is acting on a paste event. Calling Clipboard.getData() under any other circumstances will be unsuccessful."


Download: ActionScript source files and demo