現在瀏覽器種類也越來越多,諸如 IE、Firefox、Chrome、Safari等等,因此現在要實現一個js復制內容到剪貼板的小功能就不是一件那么容易的事了。
在Flash 9 時代,有一個通殺所有瀏覽器的js復制內容到剪貼板的方案:
這個方案是一個最流行的方法: 著名的Clipboard Copy解決方案 利用一個clipboard.swf作為橋梁,復制內容到剪貼板。
原理是:創建一個隱藏的flash文件,同時給給flash的變量FlashVars 賦值“clipboard=..”,通過這個賦值flash就會把復制的內容放到剪貼板。這個方法兼容IE、Firefox、Opera、chrome、 Safari,真可謂“萬能”的解決方案。瀏覽器Flash的安裝率非常高,這幾乎是一個完美的解決方案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < head > < title >Web開發者 - www.Admin10000.com </ title > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> var clipboardswfdata; var setcopy_gettext = function(){ clipboardswfdata = document.getElementById('test_text').value; //alert(clipboardswfdata); window.document.clipboardswf.SetVariable('str', clipboardswfdata); } var floatwin = function(){ alert('復制成功!'); //document.getElementById('clipinner').style.display = 'none'; } </ script > </ head > < body > < textarea id = "test_text" rows = "15" cols = "100" >文本內容.......</ textarea > < div id = "clipboard_content" > < div class = "my_clip_button" >< span class = "clipinner" id = "clipinner" >復制代碼到剪切板 < embed name = "clipboardswf" class = "clipboardswf" id = "clipboardswf" onmouSEOver = "setcopy_gettext()" devicefont = "false" src = "./_clipboard.swf" menu = "false" allowscriptaccess = "sameDomain" swliveconnect = "true" wmode = "transparent" type = "application/x-shockwave-flash" height = "20" width = "100" > </ span > </ div > </ div > </ body > </ html > |
clipboard.swf 的下載地址: clicpboard.rard
但是 Flash 10 時代,上面的方法已經不行了。
因為flash10中規定了只有在swf上進行了真實的操作(比如鼠標點擊)才能訪問剪切板,而上述方法只是使用了一個隱藏的swf文件,通過Javascript操作flash的剪貼板,用戶并沒有對swf文件進行真實的操作,因此這個方法也就失效了。
那么如何解決這個“真實操作”的問題呢?可以使用一個JavaScript庫:Zero Clipboard,利用這個js庫可以支持利用flash 10 實現復制到剪貼板。這個方法原理是在一個透明的flash(對用戶來說是不可見的)上覆蓋一個dom元素比如button或div,當點擊這個dom時,實際點擊的是flash,從而訪問flash的剪貼板。
以下是調試好的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!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 >Zero Clipboard Test</ title > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> < script type = "text/javascript" src = "ZeroClipboard.js" ></ script > < script type = "text/javaScript" > var clip = null; function $(id) { return document.getElementById(id); } function init() { clip = new ZeroClipboard.Client(); clip.setHandCursor(true); clip.addEventListener('mouseOver', function (client) { // update the text on mouse over clip.setText( $('fe_text').value ); }); clip.addEventListener('complete', function (client, text) { //debugstr("Copied text to clipboard: " + text ); alert("該地址已經復制,你可以使用Ctrl+V 粘貼。"); }); clip.glue('clip_button', 'clip_container' ); } </ script > </ head > < body onLoad = "init()" > < input id = "fe_text" cols = "50" rows = "5" value = "復制內容文本" > < span id = "clip_container" >< span id = "clip_button" >< strong >復制</ strong ></ span ></ span > </ body > </ html > |
點擊下載該例子: zeroclipboardDEMO.rar
調試時請上傳到網站,本地直接打開flash會出錯的,沒權限。zeroClipboard.js文件里moviePath屬性是falsh的地址,就是目錄下的那個ZeroClipboard.swf存放的地址位置。
這種js復制內容到剪貼板的方案可支持瀏覽器:Firefox / IE / opera / chorme / safari 所有瀏覽器!
新聞熱點
疑難解答