In Javascript, I know how to set up a drag & drop target that accepts file uploads from the user’s computer. How can I set up a drop target that accepts images that are dragged from another website? All I need to know is the URL of the image that they’ve dragged.
I know this is possible, since Google Docs accepts image drops from other websites. Any idea how they’re doing it?
UPDATE:
It looks like there are differences between Chrome on Windows and MacOS. On Windows
dataTransfer.getData('Text');works but not on MacOS.dataTransfer.getData('URL');should work on both.OLD answer:
You could define a drop zone:
and then handle the
dragenter,dragexit,dragoveranddropevents:
It is inside the
dropevent handler that we are reading the image data from thedataTransferobject as Text. If we dropped an image from some other webpage this text will represent the url of the image.And here’s a
live demo.