复制功能跨浏览器实现

来源:互联网 发布:安卓手机c语言编译器 编辑:程序博客网 时间:2024/06/06 17:23
用JS来制作网页中的HTML我们知道只能通过 window.clipboardData.setData(”Text”,meintext)来实现,这个是only for IE的,近来用alimama发现他的复杂代码功能是可以在firefox下使用的。今天一研究发现好功妙的方法,他是通过flash来实现的,代码如下:
function copy_code(meintext) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text", meintext)
  } 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="http://img.alimama.cn/images/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(meintext)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
  alert('已成功复制到剪贴板,您可以将代码加入到您的网站页面里了!');
}其实他就是判断在
window.clipboardData对像不存在的时候在页面加载一个swf然后内容通过变量传进去,通过swf来把内容放到剪贴板中。
原创粉丝点击