支持ie与FireFox的剪切板操作代码,记录几个javascript有关的小细节

支持ie与FireFox的剪切板操作代码,记录几个javascript有关的小细节

 obj.innerText alert()展现的值是 undefined。

上面是ie firefox 读取剪切板中的音讯的主意

用javascript获取客商端电脑名

function getClipboard()
{
if (window.clipboardData)
{
return (window.clipboardData.getData(‘text’));
}
else
{
if (window.netscape)
{
try
{
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();
trans.getTransferData(“text/unicode”, str, len);
}
catch (e)
{
alert(“您的firefox安全范围范围您实行剪贴板操作,请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试,相对路线为firefox根目录/greprefs/all.js”);
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;
}

if (document.all) {  //假设不是IE  
//也得以用 if(navigator.appName.indexOf(“Explorer”) > -1) // obj.innerText = “myText”;}else { obj.textContent = “yourtext”; 用textContent 代替 
}

<html xmlns=”;
<head>
<title>援助ie与FireFox的细分板代码</title>
<script type=”text/javascript”>
function setCopy(_sTxt){
try{
if(window.clipboardData){
window.clipboardData.setData(“Text”, _sTxt);
} else if(window.netscape) {//FireFox搞得真复杂呀
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’);
var str = new Object();
var len = new Object();
var str =
Components.classes[“@mozilla.org/supports-string;1”].createInstance(Components.interfaces.nsISupportsString);
var copytext = _sTxt;
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);
}
}catch(e){}
}
</script>
</head>
<body>
<input type=”button” onclick=”setCopy(‘hello’);” value=”复制” />
<div>
Firefox/3.0.14下能够运转<br />
Firefox/3.5.3下无效<br />
只是多挂念这一块了。
</div>
</body>
</html>

function copyToClipboard(txt) { 

下边是 ie,firefox下 写入剪切板的代码

  if(window.clipboardData) 
  { 
    window.clipboardData.clearData(); 
    window.clipboardData.setData(Text, txt); 
  } 
  else if(navigator.userAgent.indexOf(Opera) != -1) 
  { 
    window.location = txt; 
  } 
  else if (window.netscape) 
  { 
    try { 
        netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect); 
    } 
    catch (e) 
    { 
        alert(!!被浏览器拒绝!\n请在浏览器地址栏输入’about:config’并回车\n然后将’signed.applets.codebase_principal_support’设置为’true’); 
    } 
    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’); 
    var str = new Object(); 
    var len = new Object(); 
    var str = Components.classes[@mozilla.org/supports-string;1].createInstance(Components.interfaces.nsISupportsString); 
    var copytext = txt; 
    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); 
  } 
  return true; 

具体的应用,能够参见脚本之家的代码。

今天学习php的时候想达成获取顾客端Computer名,在asp及.net下都有落实的例子,但直接没找到办法php下完毕的艺术。后来在国外的论坛里找到了有的代码,是javascript的。但不得不运营于IE遇到,何况必得把安全等级设置得非常的低本领够。

copy2Clipboard=function(txt){
    if(window.clipboardData){
window.clipboardData.clearData();
        window.clipboardData.setData(“Text”,txt);
    }
    else if(navigator.userAgent.indexOf(“Opera”)!=-1){
        window.location=txt;
    }
    else if(window.netscape){
        try{
            netscape.security.PrivilegeManager.enablePrivilege(“UniversalXPConnect”);
        }
        catch(e){
            alert(“您的firefox安全范围范围您举办剪贴板操作,请张开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试,相对路线为firefox根目录/greprefs/all.js”);
            return false;
        }
        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’);
        var str=new Object();
        var len=new Object();
        var
str=Components.classes[“@mozilla.org/supports-string;1”].createInstance(Components.interfaces.nsISupportsString);
        var copytext=txt;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);
    }
}

function CopyToClipboard(meintext)
{
    if (window.clipboardData) 
    {
        // the IE-manier
        window.clipboardData.setData(“Text”, meintext);
    }
    else if (window.netscape) 
    { 
        // 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;
        //alert(clip);
        // maak een transferable
        var trans = Components.classes[‘@mozilla.org/widget/transferable;1’]
                        .createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

复制代码 代码如下:

恐怕用上边这段代码,成效雷同。

复制代码 代码如下:

其一函数使用在Firefox下时索要修改五个布署:
1.展开firefox ,在地点栏中输入: about:config
2.找到signed.applets.codebase_principal_support,将值设置为true

admin

网站地图xml地图