大网数据广告活动
Web 标准与前端开发

ClipboardItem: supports() static method

· 52CE 我爱测

The **`supports()`** static method of the ClipboardItem interface returns `true` if the given MIME type is supported by the clipboard, and `false` otherwise.

Clipboard API

The supports() static method of the ClipboardItem interface returns true if the given MIME type is supported by the clipboard, and false otherwise.

Note that the Clipboard API mandates support for plain text, HTML and PNG files. The supports() method will always return true for these MIME types, so testing them is unnecessary.

Syntax

supports(type)

Parameters

  • type
    • : A string indicating the MIME type to test.

      These MIME types are always supported:

      • text/plain
      • text/html
      • image/png

      These MIME types may be supported:

      • image/svg+xml
      • Custom MIME-type formats starting with "web ". The custom type (without the "web " prefix), must have the correct formatting for a MIME type.

Return value

true if the given MIME type is supported by the clipboard, false otherwise.

Examples

Writing an image to the clipboard

The following example fetches an SVG image, represents it as a Blob, and then writes it to the clipboard.

We use supports() to check whether the "image/svg+xml" MIME type is supported by the clipboard before fetching the image and writing it using clipboard.write(). We also wrap the whole function body in a try...catch statement to catch any other errors, such as ClipboardItem itself not being supported.

async function writeClipImg() {
  try {
    if (ClipboardItem.supports("image/svg+xml")) {
      const imgURL = "/my-image.svg";
      const data = await fetch(imgURL);
      const blob = await data.blob();
      await navigator.clipboard.write([
        new ClipboardItem({
          [blob.type]: blob,
        }),
      ]);
      console.log("Fetched image copied to clipboard.");
    } else {
      console.log("SVG image not supported by clipboard");
    }
  } catch (err) {
    console.error(err.name, err.message);
  }
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。