Sample javascript function to implement copy to clipboard functionality on Mozilla firefox and Internet Explorer
In Internet Explore
window.clipboardData provides access to predefined clipboard formats for use in editing operations.
window.clipboardData.setData assigns data in a specified format to the clipboardData object.
This doesn't work with Mozilla Firefox browsers.
In Mozilla Firefox
Handling Clipboard data in firefox is made more secured (and diffcult).
The most elegant and simple way is to use swf embed.
The text to be copied should be passed as parameter to swf flash file (download swf 1kb file).
The flash file would copy the text to clipboard.
function copy_to_clipboard(text)
{
if(window.clipboardData)
{
window.clipboardData.setData('text',text);
}
else
{
var clipboarddiv=document.getElementById('divclipboardswf');
if(clipboarddiv==null)
{
clipboarddiv=document.createElement('div');
clipboarddiv.setAttribute("name", "divclipboardswf");
clipboarddiv.setAttribute("id", "divclipboardswf");
document.body.appendChild(clipboarddiv);
}
clipboarddiv.innerHTML='<embed src="clipboard.swf" FlashVars="clipboard='+
encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}
alert('The text is copied to your clipboard...');
return false;
}
The flash file to be placed at path where this function is placed or else src parameter for embed tag to be changed to correct path where the flash file is been placed.
Hey, could you put an example page workin´ at somewhere here? Or maybe put the html codes that should be used with it? I found this, but couldn´t make it work. I´m newbie in javascript, and I wanna put this function at my site, but I can´t do it work. Since now, thanks!
I tried, but it didn't work.
This code is working in internet explorer but it is not working in mozilla please reply to me
Are you sure you pasted the html with the code and the swf file to your apache2 root?
This no longer works on modern browsers, IE 8 or Firefox 3+
It does not work any more with flash player 10! http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head31
this only works for ie sorry you miss 45% of the people
Yea damn that patch lol. Check out http://brooknovak.wordpress.com/2009/07/28/accessing-the-system-clipboard-with-javascript/
Thank you very much for the code. It work great to me.
I also tried but it is not working on my side also
Hello I tried this example in ie it works great but in mozilla it doesn't. Pls help me.
Works like a charm in IE but not in mozilla goshdammit!
If you have Flash 10 installed this will only work in Internet Explorer. The code for Internet Explorer uses a specific line to copy to the clipboard, while other browsers have to use a hack via Flash, which since Flash 10 no longer works :(
Hi, Thanks it worked perfectly I tested in IE6,Firefox 3.5.7 Thanks