How to Copy the contents of the clipboard in JavaScript?

By: Rajat Kumar | Last Updated: April 15, 2023

Introduction:

Copying text to the clipboard is a common operation in web applications. JavaScript provides a simple and easy way to copy text to the clipboard using the navigator.clipboard.writeText() method. In this article, we will explore how to use this method to copy the contents of the clipboard in JavaScript.

 

The navigator.clipboard.writeText() Method:

The navigator.clipboard.writeText() method is a modern browser API that allows developers to copy text to the clipboard. The method takes a single argument, which is the text to be copied. The text can be a string or any other type that can be converted to a string.

Here is an example of how to use the navigator.clipboard.writeText() method to copy text to the clipboard:

javascript code:

const textToCopy = "Hello, world!";
navigator.clipboard.writeText(textToCopy)
.then(() => {
	console.log("Text copied to clipboard");
})
.catch((err) => {
	console.error("Could not copy text: ", err);
});


In this example, we first define a variable textToCopy containing the text we want to copy. We then call the navigator.clipboard.writeText() method with textToCopy as the argument. The method returns a Promise that resolves when the text is successfully copied to the clipboard. If an error occurs, the Promise is rejected and the error is logged to the console.

 

 

Granting Permissions

Before using the navigator.clipboard.writeText() method, it is important to note that most browsers require user permission to access the clipboard. The user must explicitly grant permission to the page to access the clipboard. This permission is typically requested through a user action, such as clicking a button.

Here is an example of how to request permission to access the clipboard:

javascript code

const button = document.getElementById("copy-button");
button.addEventListener("click", () => {
    navigator.permissions.query({
            name: "clipboard-write"
        })
        .then(result => {
            if (result.state === "granted" || result.state === "prompt") {
                const textToCopy = "Hello, world!";
                navigator.clipboard.writeText(textToCopy)
                    .then(() => {
                        console.log("Text copied to clipboard");
                    })
                    .catch((err) => {
                        console.error("Could not copy text: ", err);
                    });
            }
        });
});


In this example, we first define a button element with an ID of copy-button. We then add an event listener to the button that requests permission to access the clipboard using the navigator.permissions.query() method. If permission is granted or the user is prompted to grant permission, the navigator.clipboard.writeText() method is called to copy the text to the clipboard.

 

Conclusion

Copying text to the clipboard is a common operation in web applications, and the navigator.clipboard.writeText() method provides a simple and easy way to do so. However, it is important to note that most browsers require user permission to access the clipboard, and this permission must be requested through a user action, such as clicking a button. By following the examples in this article, you should now be able to copy text to the clipboard in JavaScript.

Views