Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get data from DataTransfer.getData in event Dragover or Dragenter

Tags:

i have a code :

element.addEventListener('dragstart',function(e){    e.dataTransfer.setData('Text', 'something is here'); },false); 

when i getdata from DataTransfer.getData in event Drop is running. but when i want get data in event dragover or dragenter, data is null.

element.addEventListener('dragover',function(e){    var a = e.dataTransfer.getData('Text');    console.log(a); },false); 

So, how to get data in event dragover or dragenter

like image 832
nắng huế yêu gái huế Avatar asked Aug 10 '15 09:08

nắng huế yêu gái huế


People also ask

What is event DataTransfer 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.

Which method is used for dragged data?

Get the dragged data with the dataTransfer. getData() method. This method will return any data that was set to the same type in the setData() method. The dragged data is the id of the dragged element ("drag1")

How to getData in javascript?

The DataTransfer. getData() method retrieves drag data (as a string) for the specified type. If the drag operation does not include data, this method returns an empty string. Example data types are text/plain and text/uri-list .

Which data transfer attribute returns the specified data if there is no such data returns the empty string?

data = dataTransfer.getData(format) Returns the specified data. If there is no such data, returns the empty string.


1 Answers

Normally you don't have access to this information on events other than dragstart and drop. Firefox seems to give you access but it seems to go against the standard.

The way the data is transfered during a drag and drop is through a data store object, that contains all the information needed for the different operations to happen. But there are certain restrictions to what you can do with this information depending on the event on which you access this data store. There are 3 modes, that are defined as follow:

A drag data store mode, which is one of the following:

Read/write mode

For the dragstart event. New data can be added to the drag data store.

Read-only mode

For the drop event. The list of items representing dragged data can be read, including the data. No new data can be added.

Protected mode

For all other events. The formats and kinds in the drag data store list of items representing dragged data can be enumerated, but the data itself is unavailable and no new data can be added.

https://html.spec.whatwg.org/multipage/interaction.html#the-drag-data-store

So on dragover, the data store is in protected mode, hence the data shouldn't be available. Again, Firefox implements this differently, but you shouldn't rely on it in any case.

These modes are there for security reasons, these data transfer allows transfer not only of elements of a same page, but of data from other applications, files, etc.

like image 103
Julien Grégoire Avatar answered Sep 16 '22 11:09

Julien Grégoire