Skip to content

Instantly share code, notes, and snippets.

@spiralx
Created November 15, 2016 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spiralx/4dac5943c958059e7f234982bdc9ae59 to your computer and use it in GitHub Desktop.
Save spiralx/4dac5943c958059e7f234982bdc9ae59 to your computer and use it in GitHub Desktop.
Copy text to the clipboard
(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