Note: Many examples require HTTPS (or localhost) and a user gesture (like a click). Reading clipboard data often requires user permission. The monitor on the right updates best via its Refresh button or after copy/cut actions *on this page*. Check your browser's console (F12) for errors.
Web developers can implement copy and paste functionality using browser APIs to interact with the system clipboard. The clipboard is a temporary data buffer used for short-term data storage and transfer between applications.
Modern web development primarily utilizes the Async Clipboard API, which offers significant advantages (like asynchronous operations, better security, and richer data type support) over the older, now deprecated document.execCommand() method.
The Async Clipboard API provides a way to asynchronously read from and write to the system clipboard, preventing the browser from freezing when handling large amounts of data. It is accessed through the global navigator.clipboard object.
Clipboard: Accessible via navigator.clipboard, it contains methods like readText(), writeText(), read(), and write().ClipboardItem: Represents a single item on the system clipboard. An item can hold multiple representations (MIME types) of the same data (e.g., both plain text and HTML for rich text). Common supported types include "text/plain", "text/html", and "image/png". Support varies by browser.
The simplest way to copy text is using navigator.clipboard.writeText(text). It takes a string and returns a Promise that resolves when the text is successfully copied or rejects if it fails (e.g., due to lack of permission or not being triggered by a user gesture).
For more complex data like HTML or images, or to provide multiple formats simultaneously (like HTML with a plain text fallback), use navigator.clipboard.write(items).
This method takes an array containing one or more ClipboardItem objects. Each ClipboardItem is created with an object where keys are MIME types (strings) and values are the data in that format, typically as a Blob or a Promise that resolves to a Blob or string.
The order of MIME types within a ClipboardItem can matter, as some applications might prioritize the first type they understand. It's common practice to put the richest format (like text/html) first, followed by fallbacks (like text/plain).
Edit the content below, then copy it.
This is bold and italic text.
This copies an HTML <img> tag as text/html, with the image URL as a text/plain fallback.
Note: To copy the actual image data (not just the tag), you would fetch the image, create an image/* Blob, and use that in the ClipboardItem.
Reading requires user permission, which the browser typically prompts for on the first attempt per origin. Reading must often occur shortly after a user gesture (like clicking a "Paste" button) or when the document has focus.
Use navigator.clipboard.readText() to get the clipboard content as plain text. It returns a Promise that resolves with the text string or rejects on failure (permission denied, no text content, etc.).
Use navigator.clipboard.read() to access all available data formats. It returns a Promise that resolves to an array of ClipboardItem objects currently on the clipboard.
You then iterate through the items and, for each item, check its item.types array (a list of MIME type strings). You can request the data for a specific type using item.getType(mimeType), which returns a Promise resolving to a Blob. You can then process the Blob (e.g., read it as text using blob.text() or display it as an image using URL.createObjectURL(blob)).
Copy different types of content (text, rich text, images from a graphics editor) outside the browser, then click below to try reading it.
The Async Clipboard API has several built-in security measures:
navigator.clipboard will be undefined on HTTP pages.writeText, write) must be initiated by a user gesture (e.g., inside a click event handler). Programmatic writes outside a gesture will fail.readText, read) usually requires explicit user permission, prompted by the browser. Browsers may also restrict reading unless the page is focused or the read occurs very soon after a user interaction.<iframe> to access the clipboard, the parent page must grant permission using the allow attribute on the <iframe> tag.
This example demonstrates an iframe embedded using the srcdoc attribute. The parent document grants clipboard access via allow="clipboard-read; clipboard-write". The JavaScript *inside* the iframe can then attempt to use the Clipboard API.