那一抹温暖的阳光,是我们永恒的向往。
www.webbar.cn

JS实现复制到剪贴板(支持IE和Firefox)

JS实现复制到剪贴板(支持IE和Firefox)

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Web开发者 – www.Admin10000.com </title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<script type=”text/javascript”>
function copy_clip(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'”);
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>
</head>
<body>
<input type=”text” id=”txt” value=”www.Admin10000.com” />
<button onclick=”copy_clip(document.getElementById(‘txt’).value);alert(‘恭喜,复制成功!’);”>复制内容</button>
</body>
</html>

时间:2014-03-18

Comments are closed.

Search