Allan Jiang
Allan Jiang

Reputation: 11331

JS copying rich text from IE/Microsoft Edge

I have a button on my website, and the expectation is that when the button is clicked, a chunk of rich text (in html format) should be copied to user's clipboard, so they can then paste it to a rich text supported editor (i.e. outlook) with format.

I got it working in Chrome/FF by using multiple different approaches, such as using clipboard.js or clipboard-polyfill.js, or executing native copy command like below:

function copyToClip(str) {
      function listener(e) {
        e.clipboardData.setData("text/html", htmlstr);
        e.preventDefault();
      }
      document.addEventListener("copy", listener);
      document.execCommand("copy");
      document.removeEventListener("copy", listener);
    };

However, none of the above works in Microsoft Edge or IE. I'm wondering if it's possible to achieve the goal in Edge/IE, and if so, how should I do it?

P.S. I want to avoid using ZeroClipboard.js as much as possible because it will require user to install/enable Flash.

Thank you

Upvotes: 1

Views: 2439

Answers (2)

Rakesh Ojha
Rakesh Ojha

Reputation: 1

I used Microsoft Edge Version 83.0.478.45 (Official build) (64-bit).I tried with this code and it worked for me.

function CopyToClipboard() {
    var controlValue = "Assign Your Data";
        var textare = document.createElement('textarea');
        textare.textContent = controlValue;
        textare.style.position = "fixed";
        document.body.appendChild(textare);
        textare.focus();
        textare.select();
        try {
            window.focus();
            navigator.clipboard.writeText(textare.textContent);
        }
        catch (error) {
            console.error(error);
        }
        finally {
            document.body.removeChild(textare);
        }
 }

Upvotes: 0

Ammar Ismaeel
Ammar Ismaeel

Reputation: 803

This is working well with internet explorer

 function paste() {
            //internet explorer
            var value = window.clipboardData.getData("Text");
            document.getElementById("Text1").value = value;
        }

Upvotes: 0

Related Questions