js 跨 firefox ie 的複製,貼上功能做法
- 問題內容: Firefox實現IE下的複製貼上功能
- 感謝:
- 答案:
function cpylink(btn,obj)
{
therange=obj.createTextRange();
therange.execCommand("copy");
btn.value = " 複製完畢 ";
}
<input type="hidden" id="myurl" name="myurl" size="10" value='www.zyqRadio.com'>
<input type="button" value=" 複製本站網 址 " name="B3" onclick="javascript:cpylink(this,document.getElementById('myurl'))" >
---------------------------------------------------------------
FF裡這個問題比較麻煩,因為大部分情況FF裡處於安全考慮都會禁用複製功能,所以首先你要在FF瀏覽器的位址列裡輸 入:about:config 設定"signed.applets.codebase_principal_support"選項的值為true,然 後照下面程式碼來寫:
===============================================================================
<html>
<head>
<title>相容的複製功能</title>
<script>
function copylink(btn,inputtxt)
{
var therange=inputtxt.value;
copyToClipboard(therange);
btn.value = " 複製完畢 ";
}
function copyToClipboard(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("此操作被瀏覽器拒絕!/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;
}
</script>
</head>
<body>
<input type="hidden" id="hiddenurl" size="10" value="www.csdn.net">
<input type="button" value=" 複製本站網 址 " name="B3" onclick="javascript:copylink(this,document.getElementById('hiddenurl'))" >
</body>
</html>