Forum Moderators: open

Message Too Old, No Replies

Copy text into clipboard

         

wavesurf

11:58 am on Feb 23, 2005 (gmt 0)

10+ Year Member



I don't know if this fits into this forum, but I'll try :)

Does anyone know if it is possible have an icon that copies a certain text string into the systems clipboard.

i.e.: I have a text-string "this is the text" that the user doesn't need to see that I want to copy to the system clipboard if he/she clicks a corresponding icon (possibly image of scissors). It should then be able to paste the contents with Ctrl+V into for instance an e-mail.

Thanks for any help...

SpaceFrog

1:04 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



<div ID=textespan>here text or what ever you want</div><BR>
<A HREF=# onClick="copy2Clipboard(document.getElementById('textespan'));return(false)">Copy </A>

text can be replaced by icon <img ... />

wavesurf

1:29 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Thanks, but don't I need the function for copy2Clipboard?

However, I tried it without a declared function and it didn't seem to work.

Any more pointers?

SpaceFrog

2:13 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



oups sorry forgot the most imporant part of it
<script type="text/javascript">
function sendToClipboard(s)
{
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
alert("Internet Explorer required");
}
}
</script>

SpaceFrog

2:23 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



which gives alltogether once all the mistakes fixed:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Nouvelle page 1</title>

<script type="text/javascript">
function sendToClipboard(s)
{
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
alert("Internet Explorer required");
}
}
</script>
</head>

<body>

<div ID='textespan'>Ici le texte de votre choix!</div><BR>
<A HREF=# onclick="sendToClipboard(document.getElementById('textespan').innerHTML)">Copiez ce texte</A>
</body>
</html>

wavesurf

4:40 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Thanks, that worked almost perfect for my use. However, I try to copy a link that includes &-sign, which turns into a problem, since it translates it into &amp;, so the link doesn't work when I copy into the program I need it.

Anyone know how to fix this?

Thanks for any help?

SpaceFrog

4:47 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



you should then need to associate previous function with a replace function just before assigning innerHTML to clipBoard

ergophobe

7:03 am on Feb 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As a side note, be aware that AFAIK no clipboard script will work in Firefox or Mozilla unless

- you make a signed script
- the user has changed the security settings on the browser.

I could be wrong, but that's how I remember it from last I was playing with a similar idea.