支持ie与FireFox的剪切板代码
文章来源:365jz.com 点击数:
434 更新时间:2009-10-05 21:44
参与评论
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛
------分隔线----------------------------