1

How can you copy text to clipboard using Javascript (or even nicer a jQuery function) without involving Flash?

I don't care about IE and other browsers; Firefox 3.5 is the only browser that matters changing local FF settings is OK.

Edit: Sorry for being unclear, I did try out the first 30 methods I found via Google; none of them worked for me.

nh2
  • 24,526
  • 11
  • 79
  • 128

4 Answers4

1

A simple Google search resulted in:

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • 1
    @nh2: Strange, at least the first one worked for me. Just to be sure: did you set `signed.applets.codebase_principal_support` to `true`? But I admit my second answer didn't fit your purposes, as it used flash for FF. I added another link to an MDC component. – Marcel Korpel Mar 17 '10 at 22:10
  • 1
    You are right. I accidentally had another key of type string with the same name in my config while the actual correct boolean key was still set to false. – nh2 Apr 09 '10 at 22:23
  • This once-working way does not work any more. https://support.mozilla.org/en-US/questions/977068#answer-500083 Maybe making some noise in a Firefox bug will help us get a (safe) solution. – nh2 Feb 19 '14 at 02:22
0

You will find there are many information if you google it. eg: http://www.dynamic-tools.net/toolbox/copyToClipboard/ http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html

Geln Yang
  • 902
  • 2
  • 20
  • 36
0

By default, the access to the clipboard by firefox is disabled for security reasons. You can edit the setting in firefox to allow it:

In the firefox address bar, type in: "about:config" (no quotes), and hit enter.

In the "filter" box you now see, type in the word "signed", and you should only have one result coming up. It's set to DISABLED. Double-click it, and it should change to ENABLED. Close the window. From then on, firefox will warn you when a website tries to access your clipboard, and you can tell it to "always allow this site...."

Don Carnage
  • 71
  • 1
  • 3
  • 15
0

Now that the problem is solved, I want to add some made-up JavaScript.

  • Configure Firefox as described in the accepted answer.

Then use something like this (copied from the Mozilla resource):

function copyToClipboard(text) {

// ask for permission to access clipboard

try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch(e) {
    alert("Clipboard copying is not allowed. Set signed.applets.codebase_principal_support to 'true' in Mozilla Firefox.");
    return false;
}

// make a copy of the Unicode

var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
if (!str) return false; // couldn't get string obj
str.data = text; // unicode string?


// add Unicode & HTML flavors to the transferable widget

var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return false; //no transferable widget found

trans.addDataFlavor("text/unicode");
trans.setTransferData("text/unicode", str, text.length * 2); // *2 because it's unicode 


// copy the transferable widget!

var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
if (!clipboard) return false; // couldn't get the clipboard

clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
return true;

}
nh2
  • 24,526
  • 11
  • 79
  • 128