
It’s forbidden to generate “custom” clipboard events with dispatchEvent in all browsers except Firefox. So most browsers allow seamless read/write access to the clipboard only in the scope of certain user actions, such as copying/pasting etc. A user may switch between various applications, copy/paste different things, and a browser page shouldn’t see all that. The clipboard is a “global” OS-level thing. More about it in the specification Clipboard API and events, not supported by Firefox. It’s bit beyond our scope now, but you can find its methods in the DataTransfer specification.Īlso, there’s an additional asynchronous API of accessing the clipboard: navigator.clipboard. That’s because clipboardData implements DataTransfer interface, commonly used for drag’n’drop and copy/pasting. For instance, we can copy a file in the OS file manager, and paste it. It’s possible to copy/paste not just text, but everything. You can find more details about document selection in the article Selection and Range. So the example above uses document.getSelection() to get the selected text.


If we use event.preventDefault() it won’t be copied at all. That’s because technically the data isn’t in the clipboard yet. Please note: inside cut and copy event handlers a call to (.) returns an empty string.
