Custom Search
Logiclabz
  • Home
  • Javascript
  • Copy to Clipboard with Javascript on Mozilla firefox and IE

Copy to Clipboard with Javascript on Mozilla firefox and IE

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.


Comments

  • Nagas says:
    May 13, 09

    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!

  • Kengkaj says:
    Jun 11, 09

    I tried, but it didn't work.

  • Sreejesh says:
    Jun 18, 09

    This code is working in internet explorer but it is not working in mozilla please reply to me

  • AJ says:
    Jun 26, 09

    Are you sure you pasted the html with the code and the swf file to your apache2 root?

  • Rod Divilbiss says:
    Jun 30, 09

    This no longer works on modern browsers, IE 8 or Firefox 3+

  • namghiwook says:
    Jul 01, 09

    It does not work any more with flash player 10! http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head31

  • kx says:
    Jul 10, 09

    this only works for ie sorry you miss 45% of the people

  • jspioner says:
    Aug 02, 09

    Yea damn that patch lol. Check out http://brooknovak.wordpress.com/2009/07/28/accessing-the-system-clipboard-with-javascript/

  • Senthilnathan M says:
    Sep 02, 09

    Thank you very much for the code. It work great to me.

  • Milind says:
    Nov 05, 09

    I also tried but it is not working on my side also

  • Milind says:
    Nov 18, 09

    Hello I tried this example in ie it works great but in mozilla it doesn't. Pls help me.

  • Dudewtf says:
    Jan 03, 10

    Works like a charm in IE but not in mozilla goshdammit!

  • Alex says:
    Jan 19, 10

    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 :(

  • AJ says:
    Feb 17, 10

    Hi, Thanks it worked perfectly I tested in IE6,Firefox 3.5.7 Thanks


Leave a reply


Do you like this post?