Last active
November 5, 2019 07:47
-
-
Save JamesMGreene/8589353 to your computer and use it in GitHub Desktop.
HTML Clipboard API clarification example
This file contains 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
var btn = document.getElementById("copy-button"); | |
btn.addEventListener("click", clickHandler, false); | |
btn.addEventListener("copy", copyHandler, false); | |
function clickHandler(e) { | |
e.target.dispatchEvent(new ClipboardEvent("copy")); | |
} | |
function copyHandler(e) { | |
e.clipboardData.setData("text/plain", "Simulated copy. Yay!"); | |
// CRITICAL: Must call `preventDefault();` to get this data into the system/desktop clipboard!!! | |
e.preventDefault(); | |
} |
Hi, I am getting an uncaught type error: illegal constructor error when I try and use ClipboardEvent.
Same issue here.
You are not allowed to create constructor on ClipboardEvent in any browser but FF. Please refer to http://caniuse.com/#feat=clipboard
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello James,
thank you for your Html clipboard example, actually I'm looking for a html5 code to copy a html code like this one:
my idea is to make a button, so when I click on it, this code will be copied to the clipboard and then I can copy it anywhere in Notepad for example. just for a remarque this code is in my html body.
I have tried your example but it is only working if I copy a text not a html code.
I would very grateful if you can help to resolve this specially that I'm still new in HTML5 domain.
Best regards.