PDA

View Full Version : I want to copy text to the clipboard without an IE warning



glz
04-02-2007, 08:48 PM
Hello everyone
I'm new to javascript and I have a goal of what I want something to do, i believe I can do this with javascript but I need help with the code.

What I want to do:
I want to automatically copy text onto the clipboard without an IE or browser warning

What i've done before:

<SCRIPT LANGUAGE="JavaScript">

function ClipBoard()
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

</SCRIPT>

in body:

<SPAN ID="copytext" STYLE="height:150;width:162;background-color:pink">
This text will be copied onto the clipboard when you click the button below. Try it!
</SPAN>
<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>




2 notes about this code, it only works in IE and with a warning by the browser, in firefox it does not work at all



------------------------------------------------------------------
I have found an example site which does this and it works in IE and firefox, and does not give u a browser warning
the website is http://www.tinypic.com

how it executes this:

<div align="right"><a href="javascript:void(0)" onclick="highlight(getElementById('view_1'));"><img src="/i/copy_btn.gif" alt2"copy" width="50" height="19" border="0" /></a></div>


in the head section of the source:

function highlight(field) {
field.focus();
field.select();
var urlSwf = "j/_clipboard.swf";

window.status = 'Copied text to clipboard';

// Copy the text inside the text box to the user's clipboard
var flashcopier = 'flashcopier';
var divholder = document.createElement('div');
divholder.id = flashcopier;
document.body.appendChild(divholder);

divholder.innerHTML = '';
var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard='+escape(field.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
divholder.innerHTML = divinfo;

//alert(divinfo);

return false;
}


as you can see it executes the command to copy text with javascript, but the swf file actually does the real copying I hypthesize that this is actionscript, this in effect bypasses the browser warning and lets it work in all browsers which support swf files


now it may seem like i understand everything but I DONT, i just know how to research well, I gave you how tinypic does what it does, and I need someone to explain to me how I can execute this code to copy text for myself

i don't understand how it can reference that function with this "javascript:void(0)"


-thanks any help is appreciated