Help: Copy Image to Clipboard

K

kris_scheyer

Hi,

I've searched dozens of forums and websites to find a way to have
javascript copy an image I click on into the clipboard of windows. I
found a few examples but I couldn't get any of them to work either
getting object doesnt support this method or other errors I don't
understand. If anyone has any working code for this I'd be very
thankful for it.

And yes, I know this only works in IE and I'm assuming that the user
will be using windows.

Thanks,

Kris
 
J

Jim Davis

Hi,

I've searched dozens of forums and websites to find a way to have
javascript copy an image I click on into the clipboard of windows. I
found a few examples but I couldn't get any of them to work either
getting object doesnt support this method or other errors I don't
understand. If anyone has any working code for this I'd be very
thankful for it.

And yes, I know this only works in IE and I'm assuming that the user
will be using windows.

Well... just because they're using Windows doesn't mean they're using IE
(and just because they're using IE doesn't actually mean they're using
Windows). ;^)

But anyways...

I don't believe you can, via script, copy an image. The scriptable
clipboardData element in IE only supports text (and "url" which isn't
helpful to you either). It can't copy binary data.

Using the scriptable element like this:

if ( window.clipboardData ) {
window.clipboardData.setData("Text", "Text to copy.");
};

You could copy the URL to the image, the name of the image or any other
textual information you like about it - but not the actual image.

Jim Davis
 
K

kris_scheyer

Actually it is possible. I just found out using this code:

function CopyToClip(){
var div = document.getElementById('DivtoCopy');
div.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange) {
controlRange = document.body.createControlRange();
controlRange.addElement(div);
controlRange.execCommand('Copy');
}
div.contentEditable = 'false';
}

Just replace DivtoCopy with the div that contains whatever you want to
copy into the clipboard.

kris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top