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.



  


Leave a reply


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

  • Samuel says:
    Sep 16, 10

    Does it works on Linux? I've tested in my Kubuntu box and it doesn't work at all. Do you know anything about it?. Regards, Samuel.

  • Sivaram says:
    Sep 23, 10

    Hi, I have a similar issue, clipBoardCopy must work in Firefox. I got the javascript and i am using it, finally please any one tell me -- am i need to install Adobe Flash 10 or which version -- I am clear on Flash. regards -Sivaram

  • praveen says:
    Nov 23, 10

    it didnt work for me... pls give some other example for firefox.

  • billy says:
    Dec 15, 10

    Thanks works like a charm.

  • John says:
    Jan 21, 11

    Doesn't works on Chrome.

  • Naaaa says:
    Jan 25, 11

    Naaa Doesent work dont waist time in here

  • Kaartikeyan R says:
    Apr 26, 11

    Its not Working in Google Chrome~

  • Duc says:
    Aug 04, 11

    Thanks your code, but it only working in IE. How to fix on FF + Chrome

  • Jimmy @ fönster says:
    Sep 01, 11

    Hi great code I have searched for Firefox "copy code" a long time Thank you!

  • vestir says:
    Sep 07, 11

    Simply great runs perfectly, Thank you very much!

  • deepak says:
    Sep 15, 11

    this code is work perfectly on the ie but not work on any another broswer please help

  • Aditya Sharma says:
    Nov 16, 11

    This is not working on Mozilla.



Do you like this post?