Forum Moderators: open

Message Too Old, No Replies

Copying content (not from a textarea)

         

ahmedtheking

5:46 pm on Apr 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anyone have a simple script where by a person can click on a link and text, within that link, is copied, eg:

<a href="javascript:copythis('some content!');">click here to copy</a>.

eelixduppy

3:57 pm on Apr 24, 2006 (gmt 0)



Hello....

this is the best i can do, unfortunately it works only with internet explorer. I'm sure there is a better way, just i don't know it. Here's what i have:

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

<button onclick="sendToClipboard('Text to be copied')">
Send text to clipboard</button>
</body>
</html>

Hope this helps somewhat...

eelix

Iguana

4:26 pm on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IE specific - You could try this to copy the text of a link in a more generic way

<SCRIPT>
function CopyThis()
{
clipboardData.setData("Text", window.event.srcElement.innerText);
}
</SCRIPT>

<a href="#" onClick="Javascript:CopyThis(); return false">The text to be copied</a>

ahmedtheking

6:25 pm on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmmm, all IE specific. Lame. I've had a look at a few that 'copy from textarea' scripts and they have this function: execCommand("Copy").

I've also found this script: [dynamicdrive.com...]

And had a go at taking it apart, but I can't seem to get it to work! I made this:

function copythis(tocopy) {
therange = tocopy.createTextRange()
therange.execCommand("Copy")
window.status="Copied image's URL to clipboard!"
setTimeout("window.status=''",1800)
}

but I get an error (from javascript console (FireFox extension)) that says that 'createTextRange()' isn't defined! I donno what to do!

Iguana

6:51 pm on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think CreateTextRange is a method in Firefox. I have seen some code that claims to do it in Firefox - but it looks unecessarily complicated. I'll sticky you the link and maybe you see if it works.

You would need to first detect the browser and then apply one of the methods above to IE and different one to Firefox.