function CopyToClipboard(option) {
var textToClipboard = option.text
function CreateElementForExecCommand (textToClipboard) {
var forExecElement = document.createElement ("div");
// place outside the visible area
forExecElement.style.position = "absolute";
forExecElement.style.left = "-10000px";
forExecElement.style.top = "-10000px";
// write the necessary text into the element and append to the document
forExecElement.textContent = textToClipboard;
document.body.appendChild (forExecElement);
// the contentEditable mode is necessary for the execCommand method in Firefox
forExecElement.contentEditable = true;
return forExecElement;
}
function SelectContent (element) {
// first create a range
var rangeToSelect = document.createRange ();
rangeToSelect.selectNodeContents (element);
// select the contents
var selection = window.getSelection ();
selection.removeAllRanges ();
selection.addRange (rangeToSelect);
}
var success = true;
// Internet Explorer
if (window.clipboardData) {
window.clipboardData.setData("Text", textToClipboard);
}
else {
// create a temporary element for the execCommand method
var forExecElement = CreateElementForExecCommand(textToClipboard);
/* Select the contents of the element
(the execCommand for 'copy' method works on the selection) */
SelectContent(forExecElement);
var supported = true;
// UniversalXPConnect privilege is required for clipboard access in Firefox
try {
if (window.netscape) {
if(netscape.security && netscape.security.PrivilegeManager && netscape.security.PrivilegeManager.enablePrivilege){
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
}
// Copy the selected content to the clipboard
// Works in Firefox and in Safari before version 5
success = document.execCommand("copy", false, null);
}
catch (e) {
success = false;
alert("此操作被浏览器拒绝!/n 请在浏览器地址栏输入“about:config”并回车/n然后将[signed.applets.codebase_principal_support]设置为'true'");
}
// remove the temporary element
document.body.removeChild(forExecElement);
}
if (success) {
option.success && option.success()
}
else {
option.error && option.error()
}
}