Users Online

We have 10 guests online
Right Click - Javascript PDF Print E-mail
Written by Zack MIlls   
Tuesday, 16 March 2010 16:50

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.






  1. function copy_to_clipboard(text)
  2. {
  3. if(window.clipboardData)
  4. {
  5. window.clipboardData.setData('text',text);
  6. }
  7. else
  8. {
  9. var clipboarddiv=document.getElementById('divclipboardswf');
  10. if(clipboarddiv==null)
  11. {
  12. clipboarddiv=document.createElement('div');
  13. clipboarddiv.setAttribute("name", "divclipboardswf");
  14. clipboarddiv.setAttribute("id", "divclipboardswf");
  15. document.body.appendChild(clipboarddiv);
  16. }
  17. clipboarddiv.innerHTML='<embed src="/clipboard.swf" FlashVars="clipboard='+
  18. encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
  19. }
  20. alert('The text is copied to your clipboard...');
  21. return false;
  22. }
Last Updated on Tuesday, 16 March 2010 16:58