Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Last active August 29, 2015 14:06
Show Gist options
  • Save JamesMGreene/b709c2c57192ea39eb6b to your computer and use it in GitHub Desktop.
Save JamesMGreene/b709c2c57192ea39eb6b to your computer and use it in GitHub Desktop.
An example of discovering if a queryCommand works in a cross-browser, old-browser compliant way.
function runQueryCommand(command, showUI, value) {
var commandWorked = false,
initialDesignMode = document.designMode || "off";
try {
document.designMode = "on";
commandWorked = !!(
document.queryCommandSupported(command) &&
document.queryCommandEnabled(command) &&
document.queryCommand(command, showUI, value)
);
}
catch (e) {
// Do nothing
}
finally {
document.designMode = initialDesignMode;
}
return commandWorked;
}
if (!runQueryCommand("copy", false, undefined)) {
// Fallback to Flash-based alternative, e.g. ZeroClipboard
}
@hallvors
Copy link

I haven't seen document.queryCommand() before - you meant document.execCommand in line 10?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment