Copy Image To Clipboard From Browser In Javascript?


Answer :

No, you can't copy images to the clipboard. Copying anything to the clipboard is a security limitation of every browser, but you may able to copy text to the clipboard in IE if they have the proper security settings. Here Mozilla lists some of the problems caused by programmatic access to the clipboard.



Yes, most of the scripts supports text only.



http://forums.mozillazine.org/viewtopic.php?f=25&t=1195035&start=0



The above site also discussing the same issue.



The following site said related to security issues,



http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard



but this won't work in latest version of Mozilla.



The last answer is from 2010 and browsers have changed a lot since then.
With this simple function, you can copy whatever you want (text, images, tables, etc) (on your page) to the clipboard.
The function receive the element id or the element itself.



function copyElementToClipboard(element) {
window.getSelection().removeAllRanges();
let range = document.createRange();
range.selectNode(typeof element === 'string' ? document.getElementById(elementName) : element);
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();


}



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable