兼容主流瀏覽器的JS復(fù)制內(nèi)容到剪貼板
現(xiàn)在瀏覽器種類也越來越多,諸如 IE、Firefox、Chrome、Safari等等,因此現(xiàn)在要實現(xiàn)一個js復(fù)制內(nèi)容到剪貼板的小功能就不是一件那么容易的事了。
在FLASH 9 時代,有一個通殺所有瀏覽器的js復(fù)制內(nèi)容到剪貼板的方案:
這個方案是一個最流行的方法: 著名的Clipboard Copy解決方案 利用一個clipboard.swf作為橋梁,復(fù)制內(nèi)容到剪貼板。
原理是:創(chuàng)建一個隱藏的flash文件,同時給給flash的變量FlashVars 賦值“clipboard=..”,通過這個賦值flash就會把復(fù)制的內(nèi)容放到剪貼板。這個方法兼容IE、Firefox、Opera、chrome、 Safari,真可謂“萬能”的解決方案。瀏覽器Flash的安裝率非常高,這幾乎是一個完美的解決方案。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head>
<title>Web開發(fā)者 - www.Admin10000.com </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var clipboardswfdata;
var setcopy_gettext = function(){
clipboardswfdata = document.getElementById('test_text').value;
//alert(clipboardswfdata);
window.document.clipboardswf.SetVariable('str', clipboardswfdata);
}
var floatwin = function(){
alert('復(fù)制成功!');
//document.getElementById('clipinner').style.display = 'none';
}
</script>
</head>
<body>
<textarea id="test_text" rows="15" cols="100">文本內(nèi)容.......</textarea>
<div id="clipboard_content">
<div class="my_clip_button"><span class="clipinner" id="clipinner">復(fù)制代碼到剪切板
<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 的下載地址:http://www.jeffothy.com/weblog/uploads/clipboard.php
但是 Flash 10 時代,上面的方法已經(jīng)不行了。
因為flash10中規(guī)定了只有在swf上進(jìn)行了真實的操作(比如鼠標(biāo)點擊)才能訪問剪切板,而上述方法只是使用了一個隱藏的swf文件,通過javascript操作flash的剪貼板,用戶并沒有對swf文件進(jìn)行真實的操作,因此這個方法也就失效了。
那么如何解決這個“真實操作”的問題呢?可以使用一個JavaScript庫:Zero Clipboard,利用這個js庫可以支持利用flash 10 實現(xiàn)復(fù)制到剪貼板。這個方法原理是在一個透明的flash(對用戶來說是不可見的)上覆蓋一個dom元素比如button或div,當(dāng)點擊這個dom時,實際點擊的是flash,從而訪問flash的剪貼板。
以下是調(diào)試好的例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <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("該地址已經(jīng)復(fù)制,你可以使用Ctrl+V 粘貼。");
});
clip.glue('clip_button', 'clip_container' );
}
</script>
</head>
<body onLoad="init()">
<input id="fe_text" cols="50" rows="5" value="復(fù)制內(nèi)容文本">
<span id="clip_container"><span id="clip_button"><strong>復(fù)制</strong></span></span>
</body>
</html>
點擊下載該類庫: http://www.dbjr.com.cn/jiaoben/24961.html
調(diào)試時請上傳到網(wǎng)站,本地直接打開flash會出錯的,沒權(quán)限。zeroClipboard.js文件里moviePath屬性是falsh的地址,就是目錄下的那個ZeroClipboard.swf存放的地址位置。
這種js復(fù)制內(nèi)容到剪貼板的方案可支持瀏覽器:Firefox / IE / opera / chorme / safari 所有瀏覽器!
相關(guān)文章
調(diào)試Javascript代碼(瀏覽器F12及VS中debugger關(guān)鍵字)
目前,常用的瀏覽器IE、Chrome、Firefox都有相應(yīng)的腳本調(diào)試功能下面我就介紹如何在瀏覽器/VS中調(diào)試我們的JS代碼,感興趣的你可不要走開啊,希望本文對你有所幫助2013-01-01