65

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.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
jldupont
  • 93,734
  • 56
  • 203
  • 318

3 Answers3

29

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.

Chris S
  • 64,770
  • 52
  • 221
  • 239
peller
  • 4,435
  • 19
  • 21
  • Clipboard access: that's what I thought i.e. security risk. Thanks! – jldupont Nov 30 '09 at 10:55
  • 4
    For those wanting to look further into it, I've come across the following [code pen](http://codepen.io/netsi1964/pen/QbLLGW) which gives a pretty good breakdown of the different execCommand's available with different browsers. – Sgt Oddball Apr 28 '16 at 09:27
  • Found an exploit on the same: https://github.com/dxa4481/Pastejacking/blob/master/index.html – Mangat Rai Modi May 26 '16 at 06:00
5

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>);
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
user3798995
  • 51
  • 1
  • 2
3

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.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • How come is there some blips of it on Mozilla.org ? https://developer.mozilla.org/En/Document.execCommand – jldupont Nov 30 '09 at 01:29
  • 8
    Why would a DOM method be in a language standard? I'm pretty sure `document` or `window` are not part of the ECMA-262 standard either. – Eli Grey Nov 30 '09 at 01:47
  • 1
    @Elijah Grey: Good point. The JavaScript language is standardized by ECMA, while the DOM is standardized by the W3C. But I don't think `document.execCommand` is in the W3C DOM standard either. (A quick check of the DOM-1 spec on w3c.org seems to confirm this.) – Daniel Pryden Nov 30 '09 at 17:46
  • The question was about browser support, not the standards. – Michał Perłakowski Dec 29 '15 at 21:39