ReasonJun

navigator.clipboard.writeText() 본문

Frontend/Library

navigator.clipboard.writeText()

ReasonJun 2023. 6. 20. 13:40
728x90

The navigator.clipboard.writeText() method is a part of the Clipboard API, which allows you to access and manipulate the clipboard on your web browser. The writeText() method specifically allows you to write a string of text to the clipboard.

 

The writeText() method takes a single parameter, which is the string of text that you want to write to the clipboard. The method returns a Promise, which will be resolved when the text has been successfully written to the clipboard. If the method is unable to write the text to the clipboard, the Promise will be rejected.

 

Here is an example of how to use the writeText() method:

const text = "This is some text that I want to copy to the clipboard.";

navigator.clipboard.writeText(text).then(() => {
  console.log("Text successfully written to clipboard.");
}).catch(() => {
  console.log("Failed to write text to clipboard.");
});

In this example, the writeText() method is called with the string "This is some text that I want to copy to the clipboard." as the parameter. The method then returns a Promise, which is resolved when the text has been successfully written to the clipboard. If the method is unable to write the text to the clipboard, the Promise will be rejected.

 

The writeText() method is a powerful tool that can be used to copy text to the clipboard. It is a simple and straightforward method to use, and it is supported by most modern web browsers.

 

Here are some of the limitations of the writeText() method:

  • The method is not supported by all web browsers.
  • The method may not be able to write the text to the clipboard if the user has denied permission to access the clipboard.
  • The method may not be able to write the text to the clipboard if the user is on a device that does not support the Clipboard API.

Overall, the writeText() method is a powerful tool that can be used to copy text to the clipboard. However, it is important to be aware of the limitations of the method before using it.

728x90
Comments