经常在网页上看见“复制本页网址,给你的朋友分享”之类的话,点按钮,在IE下能复制成功,而在firefox和opera等其他浏览器就不行
下面的例子使用flash(as)+javascript实现了在不同浏览器里复制的功能,这样做的好处就是规避了浏览器兼容的问题。也就是说支持firefox,IE,OPERA
代码:
QUOTE:
<html>
<head>
<title>用flash+javscript实现网页上的文本复制</title>
</head>
<body>
<script type="text/javascript" language="javascript">
function copyit(textit) {
if (window.clipboardData) {
window.clipboardData.setData("Text",textit);
} else {
var flashcopier = 'flashcopier';
if(!document.getElementById(flashcopier)) {
var divholder = document.createElement('div');
divholder.id = flashcopier;
document.body.appendChild(divholder);
}
document.getElementById(flashcopier).innerHTML = '';
var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+textit+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
document.getElementById(flashcopier).innerHTML = divinfo;
}
}
//copyit("")
</script>
<input type="text" value="ValueA" id="a">
<a href="BLOCKED SCRIPTcopyit(document.getElementById('a').value);">复制</a>
<br />
<input type="text" value="ValueB" id="b">
<a href="BLOCKED SCRIPTcopyit(document.getElementById('b').value);">复制</a>
<br />
<input type="text" value="ValueC" id="c">
<a href="BLOCKED SCRIPTcopyit(document.getElementById('c').value);">复制</a>
</body>
</html>