Created
November 15, 2016 15:11
-
-
Save spiralx/4dac5943c958059e7f234982bdc9ae59 to your computer and use it in GitHub Desktop.
Copy text to the clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict' | |
// -------------------------------------------------------------------------- | |
window.copyToClipboard = function copyToClipboard (value) { | |
if (!value) return // Can't copy zero characters! | |
const $input = Object.assign(document.createElement('input'), { | |
type: 'text', | |
style: 'position: fixed; bottom: 0; right: -200px; width: 180px;', | |
value | |
}) | |
document.body.appendChild($input) | |
$input.select() | |
$input.focus() | |
try { | |
document.execCommand('copy') | |
$input.blur() | |
// console.info(`${value.length} characters copied to the clipboard!`) | |
} catch (ex) { | |
console.warn(ex) | |
} finally { | |
document.body.removeChild($input) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment