1

I want to execute the command:

document.execCommand("paste");

on Javascript. But it always returns only false! The console.log in the example below shows the text "false"!

document.querySelector("button").onclick = () => {
  document.querySelector("input").focus();
  console.log("execCommand('paste') returns", document.execCommand("paste"));
};
<button>paste</button>
<input type="text">
Nickolay
  • 31,095
  • 13
  • 107
  • 185
AndyMan
  • 49
  • 5

1 Answers1

0

Browsers do not generally allow web pages to access the clipboard, as it may contain sensitive information - per MDN docs for execCommand:

paste - Pastes the clipboard contents at the insertion point (replaces current selection). Disabled for web content.

An exception to this rule is when the user explicitly pastes data into your web page (via CTRL/Cmd+V or the context menu) - then you can access the clipboard contents in the paste event listener, see https://stackoverflow.com/a/12028136/1026 for details.

Nickolay
  • 31,095
  • 13
  • 107
  • 185