Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There is a couple of things you need to see. First of all, in our image you will notice there is another attribute here called data value, and its value is in quote marks I Am a Rock, and that's the payload. And down in the div for dz1, you will notice there's an attribute call dropzone, and its value is the text move and a letter asked with a colon and a mime type. This says text/plain.
HandleDragOver is simply there to make the drop work. We saw this in our last movie, that you need to intercept the dragOver event and prevent it from taking its default action in order for the drop event event to ever get fired. The dragStart event does a couple of interesting things. The first thing it does, you notice its status message has it event.target.dataset.value. And that is actually being passed into this setData method of the dataTransfer object.
So the dataTransfer object is what allows this magic to happen, with moving the payload around. So the payload exists in this event.target.dataset.value. The value of that is the value of the data-value attribute for the object that's being drag. So it will say I Am a Rock. It will be that string I Am a Rock. And so this status message will say drag started with parentheses and then it will say I Am a Rock. So we go and look at this in the browser and here we are in Google Chrome, and I am just going to press Reload, and when I pick up the rock you'll see that message come up in the status bar there.
Drag started (I Am a Rock), and so that's this event target dataset value. The dataTransfer effect Allowed, there is different things that happen depending on what the value is and we are just leaving it at move here. It could be any of move, copy or link, and it just needs to match in the other places where it's used. And in this example, the only other place where it's used is in this dropzone attribute. So this says move and that needs to match up here where it says move in order for the whole thing to work.
Now when the drop event comes around, we are going to get that data by matching this DND type. Remember, DND type is defined up here as text/plain and that gets passed when we set the data. So the data gets set with both the type and the value and when you read it later, that type has to match in order for the same value to get passed. So the handleDrop event actually receives the data from this event.datatransfer.getdata DND type.
And when it gets that data it puts it in this variable called data and it displays in the statusMessage ('received drop: ' + data). So let's take a look at that in the browsers. So when I grab this it says I Am a Rock, and when I drop it in, it says received drop: I Am a Rock. So what we've done, and this actually seems pretty simple and it is, is we have got this state of here which says I Am a rock in the data-value attribute of the image itself, and that is getting read in the dragStart event through event.target.dataset.value.
It reads that and it sets the data in the dataTransfer object. Using the datatype, text/plain, as a key, and then the drop of it comes along and reads it using that same key, and it displays the message. So go ahead and reload, look at it again. I pick this up. It say drag started (I Am a Rock) and we drop it and it says received drop: I Am a Rock. If this looks like a whole lot of work, to do something that's relatively simple, that's because that's exactly what it is.
Additionally this breaks in other browsers. Firefox doesn't support the data value attribute at all and Internet Explorer just doesn't work right. The good news is this entire technique is also not necessary. I am covering it here because its part of the spec and it's a prominent part of the spec that they actually give a whole lot of space to in the spec and I think it's important that you know what it is in case you run across it. But I am seriously suggesting you do not use this feature. Partly because it doesn't work in the two biggest marketshare browsers, and partly because there's another way to do it that is actually a whole lot more flexible and quite a bit more simple.
And we will cover about in our next movie, how to receive drops without using a dropzone.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96626 Viewers
56 Video lessons · 110343 Viewers
71 Video lessons · 79174 Viewers
131 Video lessons · 37959 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.