Last active
August 29, 2015 14:06
-
-
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.
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't seen document.queryCommand() before - you meant document.execCommand in line 10?