How to Copy Text to Clipboard With Javascript.

简体中文

Posted by Dmitry

Updated on

javascriptbrowserDOM

To copy something to the clipboard you'll need window.navigator.clipboard. Yep, that's pretty much it. Not everything must be difficult.

Here is usage example:

window.navigator.clipboard.writeText("Hello World!");

Wanna see a real life example? Click "COPY URL" at the bottom of this post.

Copy to Clipboard with navigator.clipboard.writetext Example

<button data-url-copied-text="Copied!" onclick="copyURL()" role="button">Copy</button>
function copyURL() {
  this.event.preventDefault();

  const $button = this.event.target;
  const initialButtonText = $button.text;
  const urlCopiedButtonText = $button.getAttribute('data-url-copied-text');

  window.navigator.clipboard.writeText("https://abstractkitchen.com");

  $button.text = urlCopiedButtonText;
  setTimeout(function() {
    $button.text = initialButtonText;
  }, 500);
}

I hardcoded onclick="copyURL()", but you got the idea. As you can see, I went little further and implemented UI feedback with help of data-attribute data-url-copied-text, so your fellow visitors will feel more comfortable.

Cannot read properties of undefined reading 'writeText'

The use of navigator.clipboard requires a secure origin. You can create a workaround with hidden element and document.execCommand('copy'). I remember using such hacks back in the days, when jQuery was a thing, but it's up to you. I'm not gonna dive into the details — it's already well described here.

The Latest

Quick Python Intro to OpenAI Chat Completion Functions

Quick Python Intro to OpenAI Chat Completion Functions
pythonopenaichatgpt

In this brief article, I will underline the key points and present an illustrative code snippet demonstrating how to make an API call to Wikipedia using user input.

recv() failed (104: Connection reset by peer) while reading response header from upstream

uWSGI. recv() failed (104: Connection reset by peer) while reading response header from upstream
pythonuwsgierrorsnippet

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript
javascriptcloudflaregeolocationworkers

Detect a visitor's country and city by IP address with Cloudflare Workers and JavaScript.

JavaScript Document Onload + TypeScript version

JavaScript Document Onload + TypeScript version
javascriptDOMsnippetbasicstypescript
🗃JavaScript Basics

Universal code-snippet to know when HTML is ready for modern browsers.

JavaScript addEventListener

JavaScript addEventListener
javascriptDOMsnippetbasics
🗃JavaScript Basics

How to Create Jinja2 Filters in Flask

How to Create Jinja2 Filters in Flask.
pythonflaskjinja2snippet

In this post, I'll talk about filters. Jinja2 has a list of built-in filters, and Flask leverages them.

How to Upload Files To Bunnynet Storage

How to Upload Files To Bunnynet Storage
pythonbunny_netstoragesnippet

Bunny.net is a well-known CDN and storage among developers. Here is my python snippet on how to upload a file to the storage.

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.
boilerplateopen sourceflask

Flask-Backbone is my take to create an initial structure and a set of rules, so most of my flask projects are easy to support.

How to Import CSV Files to PostgreSQL with Pgfutter

How to Import CSV Files to PostgreSQL with Pgfutter.
csvpostgresql

Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.

How to Upload Files to DigitalOcean Spaces with Python

How to Upload Files to DigitalOcean Spaces with Python
digitaloceanpython

Snippet on how to upload files to DigitalOcean Spaces with boto3 and python.