多浏览器兼容性比较好的复制到剪贴板的js代码

sochrome 2011-10-08

对于非ie浏览器他是用flash来实现的,js代码如下:

代码如下:

<script type="text/javascript"> 
function copy_code(copyText) 
{ 
if (window.clipboardData) 
{ 
window.clipboardData.setData("Text", copyText) 
} 
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="../js/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(copyText)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'; 
document.getElementById(flashcopier).innerHTML = divinfo; 
} 
alert('copy成功!'); 
} 
</script> 
<input id="inputTest" type="button" value="测试" onclick="copy_code('拷贝成功!')" />

那个flash文件从这里下载:下载

相关推荐