Is there a way...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Is there a way...

Post by 3.14 »

I have a php script which generates a code which is passed into a variable.

From this, can I create a button/hyperlink to automatically select the outputted code and copy it to clipboard? Or just select it when the button is pressed. I know it's lazy since all you have to do is select the damn thing, but this ensures no possible error due to not selecting the entire code.

Any ideas??? 8O
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

This is more of client side actually. Use this function:

Code: Select all

<script language="javascript" type="text/javascript">

function ClipBoard()
&#123;
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
&#125;

</script>
More here:
http://www.htmlgoodies.com/beyond/clipboard.html

-Nay
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

nice, now if there's a way to do it that's not only in ie i actually have a link i'd love to make copyable...
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

m3rajk wrote:nice, now if there's a way to do it that's not only in ie i actually have a link i'd love to make copyable...
I don't think it's possible for example perform this thing in Mozilla. Clipboard is a Windows-specific functionality and copying data to it is not related to HTML. This is another 'ie added function', like you said.

Regards,
Scorphus.
User avatar
Vincent Puglia
Forum Commoner
Posts: 67
Joined: Thu Sep 04, 2003 4:20 pm
Location: where the World once stood

Post by Vincent Puglia »

Hi,

could you be a bit more specific about the environment in question? eg: html code you are working with.

Vinny
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post by 3.14 »

Thanks for the help so far... I love you guys! :)

I have the following:

Code: Select all

<?php
//display the code with the remainding digits padded with X's
echo '<span ID="copytext" STYLE="height:100;width:150;" style="color: #003366;"><font face="Arial" size="4"><B>CODE IS "' . str_pad($code_so_far, 8, "X") . '"<B></font></span>';

// button for copying the code above to clipboard
?><TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy Code</BUTTON><?
?>
Now as you can see, all works fine and dandy... except it selects "CODE IS ANDHDTSX" and all I want is the "ANDHDTSX" (ANDHDTSX is for example purposes only).

Now I tried adding a span within a span to limit the selection to the str_pad($code_so_far, 8, "X") section but php didn't like that and I get a blank page. Yay. :(

So... lets play who can find a way around this loophole! First person to win gets a free lunchbox! :roll:
User avatar
Vincent Puglia
Forum Commoner
Posts: 67
Joined: Thu Sep 04, 2003 4:20 pm
Location: where the World once stood

Post by Vincent Puglia »

And where is the text supposed to go (besides the ClipBoard, which is a Windows thingy and not an IE one)

Vinny
Post Reply