Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 drag and drop between windows

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

like image 248
jordi Avatar asked Sep 12 '10 11:09

jordi


People also ask

Does HTML5 support drag and drop?

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.

Can items be dragged and dropped between application windows?

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.

How do you set dragged data in HTML5?

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.


2 Answers

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.

like image 54
Tauren Avatar answered Oct 14 '22 00:10

Tauren


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): -

  1. Check 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.
  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.

  3. Try getData('Text'). If you find a match then good. Pass on to server, etc. If not, go to step 4.
  4. You are out of luck. Show the user with an unsupported message and ask them to provide the image another way.
like image 6
acarlon Avatar answered Oct 14 '22 01:10

acarlon