Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Clipboard API in JavaScript

Copy and read text in JavaScript with navigator.clipboard.writeText and readText after button clicks. Use HTTPS; browser permission can block either operation.

IntermediateJavaScript Browser APIsExample 3 of 15

Save this as index.html and serve it on HTTPS or localhost. Localhost is a web server on your own computer. Use the page buttons to request access.

clipboard-api.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<button id="copy">Copy room code</button><button id="paste">Read clipboard</button><p id="status"></p>
7<script>
8const status = document.querySelector('#status');
9document.querySelector('#copy').onclick = async () => {
10 try {
11 if (!navigator.clipboard) throw new Error('unavailable');
12 await navigator.clipboard.writeText('LAB-204');
13 status.textContent = 'Copied LAB-204';
14 } catch { status.textContent = 'Copy failed. Use HTTPS and click the button.'; }
15};
16document.querySelector('#paste').onclick = async () => {
17 try {
18 if (!navigator.clipboard) throw new Error('unavailable');
19 const text = await navigator.clipboard.readText();
20 status.textContent = 'Clipboard: ' + text;
21 } catch { status.textContent = 'Clipboard read was not allowed.'; }
22};
23</script>
24</body>
25</html>

Sample Output

Copied LAB-204

writeText succeeded before this message was displayed.

Clipboard: LAB-204

A later allowed read found that string. Other apps may have changed it.

In this one

  • Copy a fixed string
  • Read clipboard text with permission
  • Wait for browser operations
  • Show a useful failure message

How it works

  1. 1

    Attach separate handlers to Copy and Read.

  2. 2

    Check navigator.clipboard before using it.

  3. 3

    Wait for writeText before showing Copied.

  4. 4

    Request readText only from the Read click.

  5. 5

    After copying, a permitted read shows LAB-204 unless the clipboard changed.

Edge cases

HTTP page

Clipboard may be unavailable outside a secure page.

Use HTTPS or localhost, and keep the page focused. The catch block supplies a message instead of a silent failure.

Read denied

Copy can work while reading still fails.

Reading can reveal private text, so browsers apply stricter rules. Keep a manual paste option in a real form.

What's going on

The Clipboard API copies text to the device clipboard or reads text from it. The clipboard is the temporary place used by Copy and Paste. A room-code button saves a student from selecting and copying a short code by hand.

Run this browser-only page on HTTPS or localhost. Each operation begins with its own button click. writeText stores LAB-204, while readText requests whatever text is on the clipboard now. Another application can change that text between clicks.

Both calls return a promise, which is an object for a result that will arrive later. await waits inside the async function, and try/catch handles refusal. Browsers use different permission prompts, especially for reading. Never assume that clipboard access is automatic, and never read it secretly. textContent displays the result as text, not as HTML.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

When should Copied be shown?

Pick an answer to check it.

Question 2

Which method reads a string?

Pick an answer to check it.

Questions people ask

Why have two buttons?

Each operation gets a direct user action. A later read should not rely on an earlier click still counting.

Can I read an image with readText?

No. readText reads text. Other clipboard methods handle supported non-text formats.

Why did I paste different text?

The clipboard is shared with other apps. Another copy operation may have replaced LAB-204.