当前位置:首页 > JavaScript

JavaScript对粘贴板的全操作

canca19年前 (2007-09-12)JavaScript444
<html>
    <head>
        <script language="javascript">
function copy_clip(copyObjectName){
    var con = document.getElementById(copyObjectName);
    var mytext = con.value;
    
    if (window.clipboardData){
        // IE
        window.clipboardData.setData("Text", mytext);
        // Netscape
    }else if(navigator.userAgent.indexOf("Opera") != -1) {
        alert("暂不支持Opera");
        return false;
  }else if (window.netscape){
        try{
            netscape.security.PrivilegeManager.enablePrivilege ('UniversalXPConnect');
        }catch(e){
            alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
        }
        var copytext=mytext;
        //alert("copytext set");
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        //alert("Just tried to set str")
        if (!str) return false;
        str.data = copytext;
        //alert("Just set str.data");
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].
        createInstance(Components.interfaces.nsITransferable);
        if (!trans) return false;
        //alert("Just set trans");
        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);
    }
    alert("Following info was copied to your clipboard:\n\n" + mytext);
    return false;
}

function getClipboard() {
  if (window.clipboardData) {
      return(window.clipboardData.getData('Text'));
  }
  else if (window.netscape) {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
      var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
      if (!clip) return;
      var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
      if (!trans) return;
      trans.addDataFlavor('text/unicode');
      clip.getData(trans,clip.kGlobalClipboard);
      var str = new Object();
      var len = new Object();
      try {
        trans.getTransferData('text/unicode',str,len);
      }
      catch(error) {
        return null;
      }
      if (str) {
        if (Components.interfaces.nsISupportsWString) str=str.value.QueryInterface(Components.interfaces.nsISupportsWString);
        else if (Components.interfaces.nsISupportsString) str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
        else str = null;
      }
      if (str) {
        return(str.data.substring(0,len.value / 2));
      }
  }
  return null;
}


        </script>
    </head>
    <body>
        <input type="text" value="中国人不是东亚病夫!" onclick="copy_clip('txtOne');" id="txtOne"/>
    </body>
</html>

CopyRight(C) CAnca Software Office.2007

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/555.html

标签: JavaScript
分享给朋友:

“JavaScript对粘贴板的全操作” 的相关文章

《Dom Scripting》读书笔记

  《Dom Scripting》读书笔记 Canca         最近看了一本《Jeremy Keith: DOM Scripting, Web design with JavaScript and the DOM. Apr…

javascript 中面向对象編程 (类的继承)

// 人的基類var Person=new ( function(){  var sex;  var name;  this.getSex=function() &nbs…

javascript IE与FireFox 一些兼容写法

1>获取控件用document.getElementById,不用document.all(FF等浏览器不支持)2><button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">3>使用childN…

javascrip 事件追加方法

基本方法:attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener在之前的邏輯判斷式的基礎上,在設計javascript的時候,可以針對瀏覽器的不同,寫出適合不同種類瀏覽…

javascript在IE和Firefox中的兼容考虑

1.document.formName.item("itemName") 问题说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用docum…

event事件兼容mozilla firefox的问题

本来一个写好了的脚本在IE下正常,可是在mozilla firefox中我们发现程序报类似如下的错误: event is not defined obj has no properties 原因是Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom. 下是我在网上找到的…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。