Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.execCommand to clipboard

Status
Not open for further replies.

fluxdemon

MIS
Nov 5, 2002
89
US
How do I use document.execCommand to copy a static peice of information to the clipboard using a hyperlink?
 
hi Flux,

Try this:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<script>
function CopyText()
{
document.Ftest.Ttext.select();
document.execCommand('Copy')
}
</script>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;Ftest&quot;>
<input type=&quot;text&quot; name=&quot;Ttext&quot;><br>
<a href=&quot;#&quot; onClick=&quot;CopyText();&quot;>Copy text</a><br>
<input type=&quot;text&quot; name=&quot;OtherText&quot;>
</form>
</body>
</html>

it works on IE. not NS. the Select works with NS7 but it doesn't copy it to the clipboard. I don't know if that is possible with NS. Although I have seen code that does a similar thing by calling Notepad, so that method might work with NS. Maybe someone else here knows.

I hope that helps.


Travis Hawkins
BeachBum Software
travis@cfm2asp.com
 
Thanks.... but this is not quite what I am looking for. I have set a hyperlink to lanuch onClick=&quot;document.execCommand('copy',,'&#1;');&quot; with the hope of copying &#1; to the clipboard.
 
oh...

got caried away. Just call this function with the text you want in the clipboard.

function ToClipboard(txt)
{
window.clipboardData.clearData();
window.clipboardData.setData(&quot;Text&quot;, txt);
}

That should do it.



Travis Hawkins
BeachBum Software
travis@cfm2asp.com
 
Thanks! That has gotten me closer. I'm still having a slight problem with what ends up in the clipboard though. When I pass Š to the clipboard, it gets converted to ‹. I've tried using the + operator and stringing HTML escape codes together with no luck.
 
Sorry... the posting process converted the escape codes to it's display character. I'm using the &# number ; syntax inside my code.
 
For what its worth... this works.
<%
sub sub1()
dim i, strVar
for i= 1 to 1000
strVar = chr(39) & chr(38) & chr(35) & &quot;38&quot; & chr(59) & chr(38) & chr(35) & &quot;35&quot; & chr(59) & i & chr(38) & chr(35) & &quot;59&quot; & chr(59) & chr(39)
response.write(&quot;<a href=&quot; & chr(34) & &quot;#&quot; & chr(34) & &quot; onClick=&quot; & chr(34))
response.write(&quot;copyTo(&quot; & strVar & &quot;);&quot; & chr(34) & &quot;>&quot;)
response.write(chr(38) & chr(35) & i & chr(59))
response.write(&quot;</a>&quot; & chr(10))
next
end sub
%>

<html>
<script language=&quot;javascript&quot;>
<!--
function copyTo(strVar){
window.clipboardData.setData(&quot;text&quot;,strVar);}
-->
</script>

<body>
<%sub1%>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top