is there anyway with the HTML5 Drag & Drop functionality or/and the File API to drag an jpg image from one window to another?
The idea is that I could drag and image from Facebook to a different browser's window with a custom HTML that would get that image.
Or, at least, a way to drag from the Desktop to a browser?
Thanks a lot
Complete HTML/CSS Course 2022 Now HTML 5 came up with a Drag and Drop (DnD) API that brings native DnD support to the browser making it much easier to code up. HTML 5 DnD is supported by all the major browsers like Chrome, Firefox 3.5 and Safari 4 etc.
One of the important features of Graphical User Interfaces (GUI) is drag and drop. Using a mouse you can drag and drop a file from one location to another or you may drag a file and drop it onto an application to launch it.
setData() The DataTransfer. setData() method sets the drag operation's drag data to the specified data and type. If data for the given type does not exist, it is added at the end of the drag data store, such that the last item in the types list will be the new type.
Not sure about between windows, but certainly from the desktop:
http://studio.html5rocks.com/#Photos
Actually, check out the full html5rocks.com site for ideas.
EDIT:
I just opened this demo in two separate browser windows, and I can drag from one window to the other. I was also able to drag a thumbnail from Facebook and drop it into a drop zone.
However, the Facebook image was dropped as "unknown". So it looks like you can drop from one site to another, but I'm not sure what exactly really gets dropped. If you are dragging from Facebook or such, Facebook may need to have the images or elements have the draggable
property set or something else that your application can read.
Bottom line is that you should be able to make it work between applications that you have control over. But if you are trying to integrate it with external apps, you will need to do some experimentation to find out what exactly gets passed during the drag/drop. I haven't done this work myself, but it shouldn't be hard.
This is an old one, but since I recently tussled with it and there is more to this than you might think, I put an answer in. As an introduction to learn about the native methods refer to this site.
That will get you so far. When it comes to dragging between windows, things get smelly. The problem is that once again, there isn't much consistency between browsers. Open up a bunch of different browsers and then open this excellent example. Select the first getData('Text')
radio button and test dragging and dropping images. The first thing that you will notice is that in some instances getData('Text') contains a url, but not the image url. So, now choose getData(e.dataTransfer.types[0]) based on detected content type
radio button. You will notice depending on which browser you choose that some types contain the url to the image appears, but each browser has different types. At the time of writing this some browsers (Internet Explorer) don't have any types that have the image url. This is why if you open up Google images in Internet Explorer and try that funky drag-drop image search, nothing happens - no 'drop here' box appears.
So, the best I have come up with is (make sure you refer to those links above otherwise you won't know what I am going on about): -
e.dataTransfer.files
. If there are files then all is well. This is most likely a drop from the local computer. If not, go to step 2.Loop through getData(e.dataTransfer.types[0])
and pass the url to a regex that checks for a string containing the image url. Something like:
RegExp( 'http://(.(?!http://))+?(?:[.]jpg|[.]jpeg|[.]png|[.]gif|[.]tiff|[.]ico)', 'gmi' )
If you find a match then all good. Do whatever you want, such as passing to server to retrieve image. If not, go to step 3.
getData('Text')
. If you find a match then good. Pass on to server, etc. If not, go to step 4.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With