Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
Copy rtf (Rich text format) to clipboard in javascript

For copying data to clipboard in javascript Clipboard API (clipboard.write()) should be used instead of deprecated Document.execCommand('copy').

I want to copy rtf to clipboard, which seems not supported by clipboard.write() till now in Microsoft Edge Version 98.0.1108.62:

Uncaught (in promise) DOMException: Type text/rtf not supported on write.

So I tried using the deprecated execCommand('copy') command, where I can copy to clipboard, but it is not possible to paste the RTF-content for example in Microsoft Word or another RTF-editor. I tried with text/rtf and application/rtf.

<!DOCTYPE html>
<html>

<head>
    <script>
        window.onload = function () {
            document.querySelector('button').onclick = (e) => {
                const rtf = '{\\rtf1\\ansi\\deff0' +
                    '\\trowd' +
                    '\\cellx1000' +
                    '\\cellx2000' +
                    '\\cellx3000' +
                    '\\intbl cell 1\\cell' +
                    '\\intbl cell 2\\cell' +
                    '\\intbl cell 3\\cell' +
                    '\\row' +
                    '}'
                document.oncopy = e => {
                    e.preventDefault();
                    const clipboardData = e.clipboardData;
                    clipboardData.setData('application/rtf', rtf);
                }
                document.execCommand('copy');
                document.oncopy = null;
            };

            document.querySelector('div[contenteditable]').onpaste = e => {
                e.preventDefault();
                const clipboardData = e.clipboardData;
                console.log('retrieved "%s"', clipboardData.getData('application/rtf'));
            };

        };
    </script>
</head>

<body>
    <button>copy to clipboard</button>

    <div contenteditable>paste here</div>
</body>

</html>

See also, there is only a solution for html but not rtf.

With text/html and with html-content there are some layout issues with a table, so I really have to use rtf instead of html.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!