views:

4494

answers:

2

I just stumble on a piece of code which I never saw before (granted I can't call myself a JS expert):

document.execCommand('Copy');

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

Q: Is this functionality available cross-browser?


Edit 1: I found this interesting link. The page shows a compatibility matrix for document.execCommand.

+1  A: 

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
How come is there some blips of it on Mozilla.org ? https://developer.mozilla.org/En/Document.execCommand
jldupont
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
@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
+1  A: 

This is for 'design mode' where the browser effectively turns the document into an editor. The execCommand API originated with IE. It is non-standard but has been adopted by most major browsers. Exactly which commands are supported, as well as their behavior varies across browsers. Clipboard access is considered a security risk.

peller
Clipboard access: that's what I thought i.e. security risk. Thanks!
jldupont