Sunday 30 April 2017

How do I copy to the clipboard in JavaScript?

What is the best way to copy text to the clipboard? (multi-browser)



I have tried:




function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(text);
}
}



but in Internet Explorer it gives a syntax error. In Firefox, it says unsafeWindow is not defined.



A nice trick without flash: How does Trello access the user's clipboard?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...