You can use clipboardJS , it has no dependencies and does not require Flash technology .
The only problem is support, according to the README the selection
and document#execCommand
and the browsers they support are:
- Firefox 41+
- Chrome 42 +
- Internet Explorer 9+
- Opera 29 +
basic:
In this example, whatever is set in the data-text :
new Clipboard('button');
<script src="https://raw.githubusercontent.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script><buttondata-text='Thequickbrownfoxjumpsoverthelazydog.'>copiar</button>
copyingtextfromanotherelement:
Inthisexample,whatiswritteninthetextinputwillbecopied.
new Clipboard('button');
<script src="https://raw.githubusercontent.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script><inputid='id-do-input'type='text'placeholder='Textoqueserácopiado...'/><buttondata-target='id-do-input'>copiar</button>
Youcangetsuccessorfailureinformationwhenyoutrytocopysomethingtotheclipboard.Turnontheconsolectrlshiftk)toviewoutputwhenclickingthesnippetbelow:
var clip = new Clipboard('button');
clip.on('success', function(e) {
console.info('Ação:', e.action);
console.info('Conteúdo copiado:', e.text);
console.info('Trigger:', e.trigger); // no caso de "copiar de outro elemento"
e.clearSelection();
});
clip.on('error', function(e) {
console.error('Ação:', e.action);
console.error('Trigger:', e.trigger);
});
<script src="https://raw.githubusercontent.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script>
<button data-text='Testando eventos'>copiar</button>