[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/Document/execCommand [Back]  [Original]

Document: execCommand() - Web API | MDN

MDN Web Docs

View in English Always switch to English

Document: execCommand()

Deprecated

Avoid using this feature in new projects. The execCommand() method is "not implemented consistently or fully by user agents, and it is not expected that this will change in the foreseeable future." This feature may be a candidate for removal from web standards or browsers.

Consider using the following features instead: Async clipboardcontenteditable.

:

: execCommand() DOM execCommand() document.queryCommandSupported()

execCommand contenteditable

execCommand() API

copy

: execCommand() beforeinput input execCommand() execCommand() Firefox 82 execCommand() 1634262

js
execCommand(commandName, showDefaultUI, valueArgument)

commandName

backColor

styleWithCss <color>

bold

contentReadOnly

true false

copy

href URI URI 1

cut

decreaseFontSize

<small>

defaultParagraphSeparator

delete

enableAbsolutePositionEditor

Firefox 64 (Firefox 1490641)

enableInlineTableEditing

/ Firefox 64 (Firefox 1490641)

enableObjectResizing

Firefox 64 (Firefox 1490641)

fontName

"Arial"

fontSize

1 - 7

foreColor

16

formatBlock

HTML (Firefox <blockquote> ) Edge H1H6, ADDRESS, PRE "<H1>"

forwardDelete

1 Windows Delete

heading

"H1" "H6" (Safari )

hiliteColor

color useCSS true

increaseFontSize

<big>

indent

Firefox

insertBrOnReturn

Enter <br>

insertHorizontalRule

<hr>

insertHTML

HTML TrustedHTML () HTML

: HTML DOM API (XSS)

TrustedHTML API

insertImage

() src URL createLink

insertLineBreak

insertOrderedList

insertUnorderedList

insertParagraph

insertText

()

italic

justifyCenter

justifyFull

justifyLeft

justifyRight

outdent

paste

API UI

redo

undo

removeFormat

selectAll

strikeThrough

subscript

superscript

underline

undo

useCSS

HTML CSS true/false

: false CSS true HTML styleWithCSS

styleWithCSS

useCSS true style false

AutoUrlDetect

showDefaultUI

Mozilla

valueArgument

insertImage URL null

false

: document.execCommand() true

insertText

2 HTML <textarea> <pre> contenteditable

"Bold" "Italic" insertText

HTML

html
<h2>textarea</h2>

<div class="actions" data-for="textarea">
  <button data-el="b">Bold</button>
  <button data-el="i">Italic</button>
</div>

<textarea class="editarea"></textarea>

<h2>contenteditable</h2>

<div class="actions" data-for="pre">
  <button data-el="b">Bold</button>
  <button data-el="i">Italic</button>
</div>

<pre contenteditable="true" class="editarea"></pre>

JavaScript

js
// 
const buttonContainers = document.querySelectorAll(".actions");

for (const buttonContainer of buttonContainers) {
  const buttons = buttonContainer.querySelectorAll("button");
  const pasteTarget = buttonContainer.getAttribute("data-for");

  for (const button of buttons) {
    const elementName = button.getAttribute("data-el");
    button.addEventListener("click", () =>
      insertText(`<${elementName}></${elementName}>`, pasteTarget),
    );
  }
}

// 
function insertText(newText, selector) {
  const textarea = document.querySelector(selector);
  textarea.focus();

  let pasted = true;
  try {
    if (!document.execCommand("insertText", false, newText)) {
      pasted = false;
    }
  } catch (e) {
    console.error("error caught:", e);
    pasted = false;
  }

  if (!pasted) {
    console.error("execCommand ");
  }
}

paste

<textarea> <button>

HTML

html
<button id="paste"></button>
<hr />
<textarea id="text_box"></textarea>

JavaScript

js
const pasteButton = document.querySelector("#paste");
const textBox = document.querySelector("#text_box");

pasteButton.addEventListener("click", () => {
  textBox.focus();

  let pasted = document.execCommand("paste", false);
  if (!pasted) {
    textBox.textContent =
      "execCommand ";
  }
});

API UI

W3C execCommand


Web Proxy Viewer  |  New URL  |  Original Page