Open In App

Web API Clipboard

Web API Clipboard is a powerful feature that allows web developers to interact with the user’s clipboard directly from a web application.

Concept and Usage

The Web API Clipboard provides a standardized way to interact with the user’s clipboard through JavaScript. It allows developers to perform the following key operations:



Interfaces

Web API Clipboard Methods

Two fundamental methods of the Web API Clipboard are:

Example 1: This example shows the use of the writeText() method.






navigator.clipboard.writeText('Hello, Web Clipboard!')
    .then(() => {
console.log('Text copied to clipboard');
})
    .catch(err => {
console.error('Unable to copy text: ', err);
});

Example 2: This example shows the use of the readText() method.




navigator.clipboard.readText()
    .then(pastedText => {
console.log('Pasted text: ', pastedText);
})
    .catch(err => {
console.error('Unable to paste text: ', err);
});

Browser Support


Article Tags :