Copy and paste the selected text to the clipboard using JavaScript

岁酱吖の 提交于 2019-12-01 01:57:51

Modern Day Browsers block access to the clipboard. The user has to have the security setting correct.

There are flash work-arounds, but they are not the best.

For non-IE browsers you will most likely have to use a flash solution. For IE, however, this method works perfectly:

function copyToClipboard(s) {           //only works in IE :(
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('text', s);
    }
}

no idea if this will work, but a google search yielded:

function getSel(){
  var w=window,d=document,gS='getSelection';
  return (''+(w[gS]?w[gS]():d[gS]?d[gS]):d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}

http://snippets.dzone.com/posts/show/2914

A workable cross-browser approach (minus iOS) would be to use ExternalInterface and setClipboard. So you would have a swf, flash file, that only listens to a function you call from Javascript to set the clipBoard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!