跨瀏覽器開發(fā)經驗總結(四) 怎么寫入剪貼板
更新時間:2010年05月13日 19:34:22 作者:
讓你的操作剪切板的操作支持多瀏覽器,一般IE,Firefox
IE、 Firefox可以支持JavaScript往剪貼板寫入內容
IE可以很方便的支持剪貼板內容寫入命令,可以使用execCommand(),也可以利用window.clipboardData。
使用execCommand,需要先從頁面選中要復制到剪貼板的內容,如以下代碼:
var doc = obj.createTextRange();
doc.select();
doc.execCommand('copy');
使用window.clipboardData的方法如下,代碼中同時實現了Firefox下寫入剪貼板的功能:
if(window.clipboardData) //IE
{
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
}
else if (window.netscape)
{
try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e)
{
alert("please visit 'about:config' and set signed.applets.codebase_principal_support as '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;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
以上代碼可以實現在IE、Firefox中往剪貼版寫入自定義內容,但是opera和webkit內核瀏覽器對安全性要求更高,不支持這種javascript直接操作剪貼板內容的方式,只能通過別的腳本語言“曲線救國”了。
Opera 、Safari、Chrome使用ActionScript往剪貼板寫入內容
具體的實現可以將原本的動作按鈕用flex或flash實現其外觀,替換原來的圖片或文字按鈕,然后在點擊該按鈕時,執(zhí)行以下ActionScript腳本:
//從瀏覽器環(huán)境中獲得需要寫入到剪貼板的內容
var s:String = String(ExternalInterface.call("getURL4Clipboard")); //getURL4Clipboard是頁面上return剪貼板內容的javascript方法
//設置剪貼板內容
System.setClipboard(s);
//調用完成設置剪貼板內容后需要繼續(xù)的JavaScript函數,比如提示用戶信息等
ExternalInterface.call("copyURLCompleted"); //copyURLCompleted是頁面上的javascript方法,繼續(xù)執(zhí)行復制后的工作
IE可以很方便的支持剪貼板內容寫入命令,可以使用execCommand(),也可以利用window.clipboardData。
使用execCommand,需要先從頁面選中要復制到剪貼板的內容,如以下代碼:
復制代碼 代碼如下:
var doc = obj.createTextRange();
doc.select();
doc.execCommand('copy');
使用window.clipboardData的方法如下,代碼中同時實現了Firefox下寫入剪貼板的功能:
復制代碼 代碼如下:
if(window.clipboardData) //IE
{
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
}
else if (window.netscape)
{
try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e)
{
alert("please visit 'about:config' and set signed.applets.codebase_principal_support as '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;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
以上代碼可以實現在IE、Firefox中往剪貼版寫入自定義內容,但是opera和webkit內核瀏覽器對安全性要求更高,不支持這種javascript直接操作剪貼板內容的方式,只能通過別的腳本語言“曲線救國”了。
Opera 、Safari、Chrome使用ActionScript往剪貼板寫入內容
具體的實現可以將原本的動作按鈕用flex或flash實現其外觀,替換原來的圖片或文字按鈕,然后在點擊該按鈕時,執(zhí)行以下ActionScript腳本:
//從瀏覽器環(huán)境中獲得需要寫入到剪貼板的內容
var s:String = String(ExternalInterface.call("getURL4Clipboard")); //getURL4Clipboard是頁面上return剪貼板內容的javascript方法
//設置剪貼板內容
System.setClipboard(s);
//調用完成設置剪貼板內容后需要繼續(xù)的JavaScript函數,比如提示用戶信息等
ExternalInterface.call("copyURLCompleted"); //copyURLCompleted是頁面上的javascript方法,繼續(xù)執(zhí)行復制后的工作
相關文章
淺談layui使用模板引擎動態(tài)渲染元素要注意的問題
今天小編就為大家分享一篇淺談layui使用模板引擎動態(tài)渲染元素要注意的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
Javascript動態(tài)創(chuàng)建表格及刪除行列的方法
這篇文章主要介紹了Javascript動態(tài)創(chuàng)建表格及刪除行列的方法,涉及javascript動態(tài)操作表格的相關技巧,需要的朋友可以參考下2015-05-05

