Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Last active November 5, 2019 07:47
Show Gist options
  • Save JamesMGreene/8589353 to your computer and use it in GitHub Desktop.
Save JamesMGreene/8589353 to your computer and use it in GitHub Desktop.
HTML Clipboard API clarification example
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();
}
@philipjc
Copy link

Hi, I am getting an uncaught type error: illegal constructor error when I try and use ClipboardEvent.

@cgatian
Copy link

cgatian commented Feb 12, 2016

@kresli
Copy link

kresli commented Nov 23, 2016

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