• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

204
Vistas
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 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda