Javascript: document.execCommand cross-browser?

JavascriptCross Browser

Javascript Problem Overview


I just stumble on a piece of code which I never saw before:

document.execCommand('Copy');

which seems to copy the clipboard contents to the element in focus.

Is this functionality available cross-browser?


I found a page that shows a compatibility matrix for document.execCommand.

Javascript Solutions


Solution 1 - Javascript

This is for 'design mode' where the browser effectively turns the document into an editor. The execCommand API originated with IE and was later added to HTML5. Exactly which commands are supported, as well as their behavior varies across browsers. Clipboard access is considered a security risk.

Solution 2 - Javascript

Yes, I have used it in IE, Chrome, Safari. If it works for these browser then it should work for the rest. Anyway, the execCommand method of the document object is used to execute commands relating to the built in Rich Text Editing features in the browser. The syntax of the execCommand is as follow: document.execCommand(command, uiBool, argument)

The command parameter is the command to execute - bold, underline, font, etc.

Then you have the uiBool which is the boolean value that specifies whether or not the default user interface should be shown.

And the last parameter is the argument use for some commands that requires that we pass an argument. If no argument is required by the command we pass a value of null as the third parameter.

Example:

document.getElementById("whateverID").document.execCommand('bold', false, null);

or:

document.getElementById("whateverID").document.execCommand('bold', false, <a variable nae>);

Solution 3 - Javascript

Update: Well, document.execCommand is documented in the Mozilla DOM documentation, but its description there looks slightly different from the MSDN documentation.

I'm still pretty sure it's not in the ECMA-262 standard.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjldupontView Question on Stackoverflow
Solution 1 - JavascriptpellerView Answer on Stackoverflow
Solution 2 - Javascriptuser3798995View Answer on Stackoverflow
Solution 3 - JavascriptDaniel PrydenView Answer on Stackoverflow