Skip to content

Instantly share code, notes, and snippets.

@magmoro
Created February 21, 2009 15:39
Show Gist options
  • Save magmoro/68074 to your computer and use it in GitHub Desktop.
Save magmoro/68074 to your computer and use it in GitHub Desktop.
//http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20233392.html
function CopyPlusSelect(FData) {
var dataVal = eval('document.'+FData);
dataVal.focus();
dataVal.select();
var copyText = dataVal.value;
if (window.clipboardData) { // IE send-to-clipboard method.
window.clipboardData.setData('Text', copyText);
} else if (window.netscape) {
// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// Store support string in an object.
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
if (!str) return false;
str.data=copyText;
// Make transferable.
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return false;
// Specify what datatypes we want to obtain, which is text in this case.
trans.addDataFlavor("text/unicode");
trans.setTransferData("text/unicode",str,copyText.length*2);
var clipid=Components.interfaces.nsIClipboard;
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment