Skip to content

Instantly share code, notes, and snippets.

@mderazon
Created September 27, 2017 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mderazon/9c0527ec9ecbeab8e3f6122bfef30ce1 to your computer and use it in GitHub Desktop.
Save mderazon/9c0527ec9ecbeab8e3f6122bfef30ce1 to your computer and use it in GitHub Desktop.
copy-to-clipboard-bookmarklet
var link = 'https://example.com';
var textArea = document.createElement('textarea');
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = link;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment