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.
25 comments:
Thanks for providing a fix, however how do you provide some dynamic text to the txtToCopy argument? I have some content in a hidden div that I can access with jQuery to grab the contents. I then want to copy them to the clipboard. My jquery selector would be something like this:
$("#hiddenTextToCopy").text()
I can set this to a variable but can I pass a variable to copyButton.swf?
First, you need to give unique IDs to the following elements, like so:
<PARAM NAME=FlashVars id="copyButton_param1"...>
AND
<embed src="copyButton.swf" id="copyButton_embed1"...>
Then you can do something like this:
//set vars
var txtToCopy = 'rad text';
var js = 'coolBeans()';
//make changes (must do this in both tags to be browser compatible)
$('#copyButton_embed1').attr('flashvars', 'txtToCopy='+txtToCopy+'&js='+js);
$('#copyButton_param1').attr('value','txtToCopy='+txtToCopy+'&js='+js);
This should allow for dynamic changes via jQuery
Awesome solution! Would it be possible to get the source to the swf file to style?
Russ, thanks for a great solution. I second the request for the FLA file, so we can change the button to match our app. Or, can I hire you to change it to match ours? Thanks!
I've provided the actionscript code in my post. Enjoy!
nice blog...
visit also coldfusion example
Can't download copyButton.swf! File is missing!
Thanks for letting me know about that missing file. I've fixed the problem :)
Hi,
I have a problem.
I have a textbox with inside some text and I want to capture it with your button.
What is the mod I have to do to the code?
Is it also possible to change the button text?
What is the mod I have to do to the code?
Specify the text you want copied in the FlashVars as specified above.
Is it also possible to change the button text?
To change the text, you'll have to create your own Adobe flash button using the ActionScript code I posted. You can download a trial here: http://tryit.adobe.com/us/cs4/flash/index.html
Hi, thankyou for your repply.
I spent all this morning to try to modify the code to add as text the content of the textbox id=datastring (dataString.value), but I have a lot of problem and I don't know why.
Can you please better explain the code mod?
This sounds like a problem with your own code. For one, you cannot access page elements in Javascript by calling "element_id.value". For two, you cannot put javascript into FlashVars. It just won't work.
Please look at my other comments on this post and you'll see I provided a similar solution in response to "phipps_73"
If you'd like, you can invite me to work on your project through RentAcoder.com.
Must there be this alert command in the flashvars?
So is there way to use this multiple times on a page to copy text in a form boxes ?
Or copy txt from 2 form textfields after another ? like text1+" "+text2
? n00bhere
I'll answer your questions in order:
1. no
2. yes
3. yes
I'm available on RentAcoder.com to help you out.
Thank you for help
I'll try perhaps use
http://code.google.com/p/zeroclipboard/
cause it's simple and that's why it suits for me :)
I ran into a three problems:
1. The first time the page loads it takes more than 30 secods to load in IE
2. The callback function is not working (perhaps because my snippet to be copied has /script> in it? But then you said "HTML tags" can be used. In any case escaping it to \/script> does not work either and copies over the escaped backslash as well to the clipboard as \/script?
3. In FF the top of the "COPY" button is not visible.
Here is the link to where you can see these problems hhttp://roohit.com/site/buttons99.php (the actual page would otherwise be at http://roohit.com/site/buttons.php)
Good one
But have written in my own way
Javascript Copy to Clipboard Firefox
Thanks,
I want to use this code with textareas, how it can be possible?
There isn't just one textbox in page. They have unique IDs.
ZeroClipboard is over complicated for what I need - this solution is just what I was looking for!
Do you plan to update this script so that we can do a few things:
1. Assign a CSS class to the DIV so it doesn't look like a fixed, ugly "COPY" button?
2. Have the text on the button itself (instead of "COPY") as a flash param?
Could you please share the .fla file for this?
Thanks!
This example is meant to be very basic so that you can take it and expand upon it yourself. I don't plan on doing any more updates to it. If you want something fancy, either DIY or send me a RentAcoder.com invitation.
Appreciate that. But is there any way you'd share the .fla file of at least this example, so we have a starting point? Or is all of this time-chargeable? Thanks for responding.
The Actionscript 2.0 code above is what goes into the flash file. It is really just that simple.
Thanks a lot Really Helpful
Great tip, work perfect, thanks.
Post a Comment