js复制内容到剪贴板 支持IE firfox

<script language="javascript" type="text/javascript">
<!--

// Copyright (C) krikkit - [email protected]
// --> http://www.krikkit.net/
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

function copy_clip(meintext)
{
if (window.clipboardData) 
   {
   
   // the IE-manier
   window.clipboardData.setData("Text", meintext);
   
   // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
   // het is mij echter onbekend vanaf welke versie dit precies werkt:
   }
   else if (window.netscape) 
   { 
   try { 
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
         } catch (e) { 
         alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'"); 
          }
   // dit is belangrijk maar staat nergens duidelijk vermeld:
   // you have to sign the code to enable this, or see notes below 
   //netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   // maak een interface naar het clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // maak een transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specificeer wat voor soort data we op willen halen; text in dit geval
   trans.addDataFlavor('text/unicode');
   
   // om de data uit de transferable te halen hebben we 2 nieuwe objecten 
   // nodig om het in op te slaan
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   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);
   
   }
alert("注册地址和你的ID已复制到剪贴版,你可按CTRL+V在任何软件如:QQ、MSN中进行粘贴推荐好友注册");

   return false;
}

function copyUrl(user_id,ad3){
if(user_id!=""&&(ad3>2)){
var content=""; 
content+="http://club.sohu.com/reg_sohu.php?commend_cn="+user_id; 
copy_clip(content);
//window.clipboardData.setData("Text",content);
//alert("注册地址和你的ID已复制到剪贴版,你可按CTRL+V在任何软件如:QQ、MSN中进行粘贴推荐好友注册。");
}else{
alert("很抱歉,您的社区等级还不够3级或3级以上,不能做为推荐人。");
}
return false;
}
//-->
</script>

<a style="cursor:pointer;text-decoration:underline" onclick="javascript:copyUrl('iew000fdjlsfj@sohu',3);">推荐好友注册</a>
<a style="cursor:pointer;text-decoration:underline" onclick="javascript:copy_clip('推荐好友测试');">推荐好友注册</a>

你可能感兴趣的:(IE)