---
title: "navigator.clipboard: The Async Clipboard API in JavaScript"
description: "Copy and paste text, images, and files using the new navigator.clipboard API"
canonical: "https://www.trevorlasn.com/blog/javascript-navigator-clipboard"
author: "Trevor I. Lasn"
published: "2024-12-07"
last-updated: "2026-11-08"
topics: ["JavaScript"]
---

# navigator.clipboard: The Async Clipboard API in JavaScript

Copy and paste text, images, and files using the new navigator.clipboard API

By Trevor I. Lasn · Published 2024-12-07 · Updated 2026-11-08 · https://www.trevorlasn.com/blog/javascript-navigator-clipboard

When I first encountered the `navigator.clipboard` API, I was amazed by how simple it made copying and pasting in web applications. Gone were the days of hacky `document.execCommand` solutions.

Before the Clipboard API, we relied on `document.execCommand("copy")` to handle clipboard operations. This approach was synchronous and limited - it could only copy text from selected DOM elements. Plus, it didn't work consistently across browsers.

The Clipboard API reached Baseline Newly Available status in June 2024 and is now supported across all modern browsers.

[Browser support for `async-clipboard` on webstatus.dev](https://webstatus.dev/features/async-clipboard)

The `navigator.clipboard` API solves these issues by providing asynchronous methods that work with various data types. It's more secure too - browsers require explicit permission before allowing clipboard access.

```javascript title="JavaScript"
try {
  await navigator.clipboard.writeText('Hello from clipboard!');
  console.log('Text copied');
} catch (err) {
  console.error('Failed to copy:', err);
}
```

Reading text works similarly:

```javascript
try {
  const text = await navigator.clipboard.readText();
  console.log('Pasted content:', text);
} catch (err) {
  console.error('Failed to paste:', err);
}
```

Copying complex data like images or files:

```javascript
try {
  const imageBlob = new Blob(['<image data>'], {type: 'image/png'});
  const item = new ClipboardItem({
    'image/png': imageBlob
  });
  await navigator.clipboard.write([item]);
} catch (err) {
  console.error('Failed to copy image:', err);
}
```

Note that clipboard operations need permission and HTTPS in most browsers. Error handling is key since operations can fail if permission is denied or in restricted environments.

### Browser Support Considerations

While clipboard API support is universal in modern browsers, you may want a fallback for legacy browsers. Note that `document.execCommand` is deprecated:

```javascript title="JavaScript"
if (navigator.clipboard) {
  // Use the new clipboard API
} else {
  // Fallback to execCommand (old)
  const textArea = document.createElement('textarea');
  textArea.value = text;
  document.body.appendChild(textArea);
  textArea.select();
  try {
    document.execCommand('copy');
  } finally {
    document.body.removeChild(textArea);
  }
}
```

<br />

### When to Use `navigator.clipboard` API

The async clipboard API shines in several common scenarios:
- Copy-to-clipboard buttons in documentation
- Saving code snippets from editors
- Moving rich content between applications
- Handling image uploads via paste

The API's permission model might seem like extra overhead, but it's a worthwhile trade-off for security and reliability. Gone are the days of wrestling with text selection and synchronous clipboard operations.

Next time you need to implement clipboard functionality, reach for `navigator.clipboard` 

Remember to add fallbacks for older browsers, and your users will thank you for the smooth experience.
