Copy to Clipboard
Copy any text to clipboard in a browser. Usage:
var copy = ; document;
Features:
- The smallest possible function to copy to clipboard in a browser
- No flash.
- No frameworks.
- No dependencies, not even
npm
dependencies. - Just 20 lines of code
- Why? Because even 3Kb gzipped is too much for a simple
copy-to-clipboard
function. - Execute inside a user generated event.
- Function may
throw
on error, so wrap intry {} catch() {}
block. - Inspired by clipboard.js.
- Use the second parameter to tell where to insert the invisible
<input>
so that scroll does not move when it is focused.
Tip
Save yourself time and just copy this function straight into your project:
{ var doc = document; parent = parent || docbody; var el = doc; // Remove from screen. var style = elstyle; styleposition = 'absolute'; stylewidth = 1; styleheight = 1; styleopacity = 001; styleleft = -9999; // Prevent zooming on iOS stylefontSize = '12pt'; // Exec `copy` command. elvalue = text; parent; el; el; doc; parent;}