# How to Copy to Clipboard in Firefox and other browsers other than IE?



## debiprasad_sahoo (Jan 6, 2008)

Dear friends, I need to copy some contents to clipboard on the click event of a button. For IE the js code is as follow

```
window.clipboardData.setData('Text', 'the text you need to copy');
```

For firefox there is some complex code, which can copy to clipboard, but it is not working correctly. It shows this error


> uncaught exception: A script from "*loveorkut.com/" was denied UniversalXPConnect Privilages.


The js code is as follow

```
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                     .createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;
   
        trans.addDataFlavor('text/unicode');
   
        var str = new Object();
        var len = new Object();
        
        var str = Components.classes["@mozilla.org/supports-string;1"]
                    .createInstance(Components.interfaces.nsISupportsString);
       
        var copytext = "The Text you want to copy";
       
        str.data = copytext;
        
        trans.setTransferData("text/unicode", str, copytext.length*2);
        
        var clipid=Components.interfaces.nsIClipboard;
        
        if (!clip) return false;
       
        clip.setData(trans, null, clipid.kGlobalClipboard);
```
However this code works in local, it asks for a confirmation only to give access to the clipboard. I need this to be worked in Internet also. Any other good code will be better. Please help.

If you have any questions or ideas, then please discuss here.


----------



## QwertyManiac (Jan 7, 2008)

Mozilla doesn't allow copying to clipboard unless the script is signed (Digital signatures, from Verizon, etc.). And even if its signed it asks for user permission before allowing the script to do so.

But there is one hack, it uses a flash object to perform the same task and doesn't require permissions, etc. You may use it from here: *www.jeffothy.com/weblog/clipboard-copy/

(P.s. There is a setting in About:Config which allows script to copy to clipboard, but it makes no sense asking a user to change all those defaults, etc.)

Or go the Mozilla's ugly and long way: *developer.mozilla.org/en/docs/Using_the_Clipboard


----------



## Lucky_star (Jan 7, 2008)

There is this flash component which allows copying to clipboard from any browsers. Its not designed by me. But I had used it once and its working in almost all browsers I have tested. I am not getting the exact source of this file. But its released under the GPL, so freely distributable.

Download this file: *rapidshare.com/files/81866988/_clipboard.swf.html
Keep it in the same folder.

Here is the code:



```
<script type="text/javascript">
function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
    }
}

</script>
```
Now pass any _parameters_ to the function copy(_parameters_) and it will be copied to the clipboard instantly.

Like this:

```
<input type="button" onClick="copy('ThinkDigit Forum')">
```
This will copy _Thinkdigit Forum_ to the Clipboard.
Tell me if you have any problems.


----------



## Gigacore (Jan 7, 2008)

^ yup.. it must work !


----------



## debiprasad_sahoo (Jan 26, 2008)

Lucky_star said:


> Tell me if you have any problems.


I was bit busy. That's is why I can not test it. Today I tested it. It works fine in IE, Firefox and Opera. Thank you.


----------

