PDA

View Full Version : Copying To Clipboard with pre-defined text! (JavaScript & IE)


Xula
04-06-2005, 03:55 AM
I've been looking for this for a bit now, asking person after person to no avail.

Here's the question I've been passing along, thanks to all who just read this

Anyone know how to copy text to the clipboard that is predefined?

<script language="JavaScript">
<!--
function CopyToClipBoard(post) {
polka = createTextRange(post);
polka.execCommand("Copy");
}
-->
</script>

is what I want, but that is apparently not working/I'm doing something wrong/haven't used javascript much...

all the examples online use forms for the data, such as:

function copyText(theText) {
var tempval=eval("document."+theText)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
alert("Text copied into the clipboard!");

but I want to do it purely with the data passed through the value 'post'...

Anyone have any ideas?

And if you get this, you can be so happy you stumped me, many friends, and two college-level teachers that I have asked

Thanks!

[Z]
04-06-2005, 01:06 PM
<script language="JavaScript">

//copy predefined text or value to clipboard
function copyIt_1(x){
window.clipboardData.setData('Text',x)}
copyIt_1('_some_predefined_text_or_value')

//concat text contained within the range
function copyIt_2(x){
r=document.selection.createRange().text;
window.clipboardData.setData('Text',(r+x))}
copyIt_2('_and_some_predefined_text_or_value')

</script>

rlemon
04-06-2005, 03:04 PM
you know the execCommand(copy) does not work in mozilla.

Xula
04-06-2005, 07:10 PM
Thank you for the response! I tried using your code... and unfortunately it's still not working.

Internet Explorer does not report any errors, but every time I click the link, javascript:CopyToCB('thisinformation'), it doesn't. The function I used, (From yours), is:
function CopyToClipBoard(post) {
//copy predefined text or value to clipboard
window.clipboardData.setData('Text',post)}
}

I have no idea why it's not working! Any other thoughts?

--and yes, I know this will not work in Mozilla! Thanks.

thanks for the reply! :)