javascript实现剪切板功能
1 <script type="text/javascript"> 2 function copyToClipBoard(clipBoardContent) { 3
if(window.clipboardData){ 4 window.clipboardData.clearData(); 5 window.clipboardData.setData("Text", clipBoardContent); 6 }else if(navigator.userAgent.indexOf("Opera") != -1){ 7 window.location = clipBoardContent; 8 }else if (window.netscape){ 9 try{ 10 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 11 }catch (e){ 12 alert("您的当前浏览器设置已关闭此功能!请按以下步骤开启此功能!/n新开一个浏览器,在浏览器地址栏输入'about:config'并回车。/n然后找到'signed.applets.codebase_principal_support'项,双击后设置为'true'。/n声明:本功能不会危极您计算机或数据的安全!"); 13 } 14 var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); 15 if (!clip) return; 16 var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); 17 if (!trans) return; 18 trans.addDataFlavor('text/unicode'); 19 var str = new Object(); 20 var len = new Object(); 21 var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 22 var copytext = clipBoardContent; 23 str.data = copytext; 24 trans.setTransferData("text/unicode",str,copytext.length*2); 25 var clipid = Components.interfaces.nsIClipboard; 26 if (!clip) return false; 27 clip.setData(trans,null,clipid.kGlobalClipboard); 28 } 29 alert("已成功复制!"); 30 return true; 31 }
}