View Full Version : Copy Textarea content to clipboard
jnscollier
01-20-2008, 09:21 PM
For some reason I'm having a hard time finding this. I'm looking for a script that would allow users to click in a textarea box and it would automatically copy to clipboard whatever is in the box. Preferably it would say "copied" too so that the user knows that it was copied. (Just like photobucket) All the scripts I seem to find require a button/link or don't work in firefox.
Is there a script that works in both IE and Firefox? Any help would be appreciated. Thanks soooo much! :cool:
djr33
01-20-2008, 09:54 PM
I'm not aware of a script that will copy to the clipboard. Might be possible using ActiveX in IE only.
But you can do a select all operation automatically, then have the user copy himself. You can find this select all script in the DD library I believe.
jnscollier
01-20-2008, 10:20 PM
Yeah all the scripts i found have some button or a link that says "copy" and then the user clicks it and it copies whatever is in the textarea. But, I saw that photobucket has it so you just click in the textbox and it copies the data automatically...Anyone know how this is done?
Some of the scripts I have found don't even work in firefox and only work in IE so I don't know how photobucket does it...
jnscollier
01-20-2008, 11:04 PM
Hey all,
So I found this:
<script language="javascript">
function copy(inElement) {
if (inElement.createTextRange) {
var range = inElement.createTextRange();
if (range)
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 = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
document.getElementById(flashcopier).innerHTML = divinfo;
}
}
</script>
<form name="formtocopy" action="" >
<textarea name="texttocopy" disabled="disabled">
A whole bunch of text here that will be copied.
</textarea>
<br>
<a href="javascript:copy(document.formtocopy.texttocopy);">Copy the Text!</a>
</form>
This apparently does work across diff browsers and it's not exactly what i wanted but it will do. Anyway, is there a way i can write "copied..." right next to the button when it is clicked. indicating to the user that it has been copied). I know this is probably a dumb question but I don't know how to do that.
Aaron
01-21-2008, 07:42 AM
I have that programmed well (not pulling from the text area, doing it hardcode with PHP) but am sure you can do it just like photobucket. In fact, I need to update my code to make that much nicer feature, improve the language in my clipboard copy. I have popup alert() working too. Would prefer the CSS thing that is done by Photobucket. Perhaps someone can do that and report back to me and I'll implement it in my website.
http://www.jeffothy.com/weblog/clipboard-copy/
I discovered it from reading blog of guy who created 2Prong. That blog is not working a the moment, not sure why. "webchicanery.com"
(too bad nobody answers my questions in this forum .... hint)
jnscollier
01-26-2008, 05:42 PM
yeah i hear ya. the code on that jeff site was good but my problem with that was that there was no indication it "copied" to the user. otherwise it works perfectly. anyway, i ended up just finding a script that just selects all and the user will have to copy and paste.
BLiZZaRD
01-26-2008, 06:13 PM
unless you want it in Flash, I don't see any other way except user interaction....
Aaron
01-27-2008, 07:39 AM
I'm surprised you can't get it to work by just clicking in the textarea? Isn't there a javascript that would detect such and you can use to trigger the clipboard copy JS? I was using <a> tag and when someone clicks on it he clipboard copy would occur, and the alert() box would tell user that info was just copied to their clipboard.
If anyone knows of JS that can detect when user clicks into a Text Area, please post in this thread. I havn't done google search.
FYI, I am using Dynamic Drive FormLimiter script, by Twey, but it seems to have disappeared? It monitors the text area input size. Perhaps you can check that out to see how it's programmed, maybe it will have a trick you can use?
djr33
01-27-2008, 12:03 PM
onFocus="....." will activate when you click into a form field.
That link is interesting, though reliant upon flash.
Shasoosh
04-11-2008, 07:53 PM
hey guys, i came across this thread while searching the net for the same thing but what i want to accomplish is to copy to the clipboard, using the above script, the webpage current URL. is that possible? i've tried to play with the form above location.href etc' but with no luck.
thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.