js实现支持多浏览器(IE、Firefox、Opera)剪切板复制函数

发布时间:2020-11-19编辑:脚本学堂
本文介绍下,javascript实现的可以支持多浏览器(IE、Firefox、Opera)剪切板的复制函数,供大家学习参考。

注意:
firefox下必须要设置:
signed.applets.codebase_principal_support
在 firefox浏览器地址栏输入about:config,过滤器中输入signed.applets.codebase_principal_support双击设置为true即可支持。
原因在于:firefox默认情况下是不支持脚本操作剪切板的。什么都不支持的浏览器才是最安全的浏览器,你认为呢?!

1,代码如下:

<script> 
function copyToClipboard(txt) { 
if(window.clipboardData) { 
window.clipboardData.clearData(); 
window.clipboardData.setData("Text",txt); 
alert("复制成功!"); 
} 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'之后 
重试,具体可以参考http://www.jb200.com/article/10019.html"); 
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); 
} 
} 
copyToClipboard("http://www.jb200.com"); 
</script>

2,ie firefox 读取剪切板中信息的方法。

<script>
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;
}
</script>

3,ie,firefox下 写入剪切板的代码。

<script>
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);
}
}
</script>