HTML5 alternative to flash-based ZeroClipboard for safe copying of data to clipboard?

前端 未结 6 1473
春和景丽
春和景丽 2020-12-02 14:22

With flash on the way out in many environments (iPhone, Android, IE10, etc...), is there any new solution forthcoming in any browsers that will allow a safe copy of informat

6条回答
  •  抹茶落季
    2020-12-02 14:36

    I have created a pure JavaScript solution called clip-j. Here it is. Basically what it does is it takes advantage of document.execCommand('copy'); with a few other commands that make it so you don't see anything. Here's the code:

    function clip(text) {   
        var copyElement = document.createElement('input');      
        copyElement.setAttribute('type', 'text');   
        copyElement.setAttribute('value', text);    
        copyElement = document.body.appendChild(copyElement);   
        copyElement.select();   
        document.execCommand('copy');   
        copyElement.remove();
    }
    

提交回复
热议问题