Clearing the Clipboard
 

Clearing the Clipboard

Looking at my pages on preventing the Context Menu and the oncopy Event I see that I still need to clear the Windows (and probably other OS's) clipboard.

An AI result revealed:
Clipboard API: The navigator.clipboard.writeText('') writes an empty string to the clipboard, effectively clearing it.

Select some text from somewhere on the screen.

Try to copy some text here

Check the the Clipboard clears

This seems to be similar to my example of Adding a URL to a Clipboard Copy.

Top

the AI code:

To clear the clipboard using HTML and JavaScript, you can use the Clipboard API. Here's an example:

Explanation:
1. Button: A button is provided for the user to trigger the clipboard clearing action.
2. Clipboard API: The navigator.clipboard.writeText('') writes an empty string to the clipboard, effectively clearing it.
3. Error Handling: If the operation fails (e.g., due to browser restrictions), an error message is logged, and the user is notified.

This approach works in modern browsers that support the Clipboard API. If you're testing this locally, ensure the page is served over HTTPS, as clipboard operations often require a secure context.

Clipboard API

The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste), as well as to asynchronously read from and write to the system clipboard.

Links

Link to PDF Disabled

If you require a copy please email tempusfugit.me.uk

Top

References - a note on these

  • The Window Navigator Object - https://www.w3schools.com/jsref/obj_navigator.asp
  • Clipboard API - https: //developer.mozilla.org/en-US/docs/Web/API/ Clipboard_API

Site design by Tempusfugit Web Design -