Created
April 16, 2015 16:39
-
-
Save GeoffCox/4b30910df4ea0d10e55a to your computer and use it in GitHub Desktop.
Copy HTML to clipboard in any browser
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
// It may be worth making a copy of the element within <body> that is hidden | |
// or even producing a section of items to copy. | |
// This way you can remove any interactivity styles that make the copied content not | |
// look good when pasted into other applications. | |
var element = document.getElementById('ClipboardTemp'); | |
// This technique is simulating the user selecting these elements. | |
// Think of a ControlRange as a selection of controls and then executing the command like the user pressing 'Ctrl+C' | |
element.contentEditable = 'true'; | |
var controlRange; | |
if (document.body.createControlRange) { | |
controlRange = document.body.createControlRange(); | |
controlRange.addElement(element); | |
controlRange.execCommand('Copy'); | |
} | |
element.contentEditable = 'false'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment