DataTransfer

The DataTransfer object is used to hold the data that is being dragged during a drag and drop operation. It may hold one or more data items, each of one or more data types. For more information about drag and drop, see Drag and Drop.

Please add a summary to this article.
  Last changed in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

This object is available from the dataTransfer property of all drag events. It cannot be created separately.

Property overview

Properties Type
dropEffect String
effectAllowed String
files FileList
mozCursor String
mozItemCount unsigned long
mozSourceNode Node
mozUserCancelled Boolean
types StringList

Method overview

void addElement(in Element element)
void clearData([in String type])
String getData(in String type)
void setData(in String type, in String data)
void setDragImage(in nsIDOMElement image, in long x, in long y)
void mozClearDataAt([in String type, in unsigned long index])
nsIVariant mozGetDataAt(in String type, in unsigned long index)
void mozSetDataAt(in String type, in nsIVariant data, in unsigned long index)
StringList mozTypesAt([in unsigned long index])

Properties

dropEffect

The actual effect that will be used, and should always be one of the possible values of effectAllowed.

For the dragenter and dragover events, the dropEffect will be initialized based on what action the user is requesting. How this is determined is platform specific, but typically the user can press modifier keys to adjust which action is desired. Within an event handler for the dragenter and dragover events, the dropEffect should be modified if the action the user is requesting is not the one that is desired.

For dragstart, drag, and dragleave events, the dropEffect is initialized to "none". Any value assigned to the dropEffect will be set, but the value isn't used for anything.

For the drop and dragend events, the dropEffect will be initialized to the action that was desired, which will be the value that the dropEffect had after the last dragenter or dragover event.

Possible values:

  • copy: A copy of the source item is made at the new location.
  • move: An item is moved to a new location.
  • link: A link is established to the source at the new location.
  • none: The item may not be dropped.

Assigning any other value has no effect and retains the old value.

effectAllowed

Specifies the effects that are allowed for this drag. You may set this in the dragstart event to set the desired effects for the source, and within the dragenter and dragover events to set the desired effects for the target. The value is not used for other events.

Possible values:

  • copy: A copy of the source item may be made at the new location.
  • move: An item may be moved to a new location.
  • link: A link may be established to the source at the new location.
  • copyLink: A copy or link operation is permitted.
  • copyMove: A copy or move operation is permitted.
  • linkMove: A link or move operation is permitted.
  • all: All operations are permitted.
  • none: the item may not be dropped.
  • uninitialized: the default value when the effect has not been set, equivalent to all.

Assigning any other value has no effect and retains the old value.

files

Contains a list of all the local files available on the data transfer. If the drag operation doesn't involve dragging files, this property is an empty list. An invalid index access on the FileList specified by this property will return null.

Example

This example dumps the list of files dragged into the browser window.

Unable to find file filednd.html

See this in effect at:  http://jsfiddle.net/9C2EF/

types

Holds a list of the format types of the data that is stored for the first item, in the same order the data was added. An empty list will be returned if no data was added.

Gecko 1.9.2 note
The string "Files" is included in this list if files are included in the drag.

(Firefox 3.5 / Thunderbird 3.0 / SeaMonkey 2.0)

mozCursor

The drag cursor's state. This is primarily used to control the cursor during tab drags.

Note: This method is currently only implemented on Windows.

Possible values

auto
Uses the default system behavior.
default
Uses the default Gecko behavior, which is to set the cursor to an arrow during the drag operation.
Note: If you specify any value other than "default", "auto" is assumed.

mozItemCount

The number of items being dragged.

Note: This property is Gecko-specific.

mozSourceNode

The Node over which the mouse cursor was located when the button was pressed to initiate the drag operation. This value is null for external drags or if the caller can't access the node.

Note: This property is Gecko-specific.

mozUserCancelled

This property applies only to the dragend event, and is true if the user canceled the drag operation by pressing escape. It will be false in all other cases, including if the drag failed for any other reason, for instance due to a drop over an invalid location. This property is not currently implemented on Linux.

Note: This property is Gecko-specific.

Methods

addElement()

Set the drag source. Usually you would not change this, but it will affect which node the drag and dragend events are fired at. The default target is the node that was dragged.

 void addElement(
   in Element element
 );
Parameters
element
The element to add.

clearData()

Remove the data associated with a given type. The type argument is optional. If the type is empty or not specified, the data associated with all types is removed. If data for the specified type does not exist, or the data transfer contains no data, this method will have no effect.

 void clearData(
   [optional] in String type
 );
Parameters
type
The type of data to remove.

getData()

Retrieves the data for a given type, or an empty string if data for that type does not exist or the data transfer contains no data.

A security error will occur if you attempt to retrieve data during a drag that was set from a different domain, or the caller would otherwise not have access to. This data will only be available once a drop occurs during the drop event.

 void getData(
   in String type
 );
Parameters
type
The type of data to retrieve.

setData()

Set the data for a given type. If data for the type does not exist, it is added at the end, such that the last item in the types list will be the new format. If data for the type already exists, the existing data is replaced in the same position. That is, the order of the types list is not changed when replacing data of the same type.

 void setData(
   in String type,
   in String data
 );
Parameters
type
The type of data to add.
data
The data to add.

setDragImage()

Set the image to be used for dragging if a custom one is desired. Most of the time, this would not be set, as a default image is created from the node that was dragged.

If the node is an HTML img element, an HTML canvas element or a XUL image element, the image data is used. Otherwise, image should be a visible node and the drag image will be created from this. If image is null, any custom drag image is cleared and the default is used instead.

The coordinates specify the offset into the image where the mouse cursor should be. To center the image, for instance, use values that are half the width and height of the image.

 void setDragImage(
   in Element image,
   in long x,
   in long y
 );
Parameters
image
An element to use as the drag feedback image
x
Horizontal offset within the image.
y
Vertical offset within the image.

mozClearDataAt()

Removes the data associated with the given format for an item at the specified index. The index is in the range from zero to the number of items minus one.

If the last format for the item is removed, the entire item is removed, reducing mozItemCount by one.

If the format list is empty, then the data associated with all formats is removed. If the format is not found, then this method has no effect.

Note: This method is Gecko-specific.
 void mozClearDataAt(
   [optional] in String type,
   in unsigned long index
 );
Parameters
type
The type of data to remove.
index
The index of the data to remove.

mozGetDataAt()

Retrieves the data associated with the given format for an item at the specified index, or null if it does not exist. The index should be in the range from zero to the number of items minus one.

Note: This method is Gecko-specific.
 nsIVariant mozGetDataAt(
   [optional] in String type,
   in unsigned long index
 );
Parameters
type
The type of data to retrieve.
index
The index of the data to retrieve.

mozSetDataAt()

A data transfer may store multiple items, each at a given zero-based index. mozSetDataAt() may only be called with an index argument less than mozItemCount in which case an existing item is modified, or equal to mozItemCount in which case a new item is added, and the mozItemCount is incremented by one.

Data should be added in order of preference, with the most specific format added first and the least specific format added last. If data of the given format already exists, it is replaced in the same position as the old data.

The data should be either a string, a primitive boolean or number type (which will be converted into a string) or an nsISupports.

Note: This method is Gecko-specific.
 void mozSetDataAt(
   [optional] in String type,
   in nsIVariant data,
   in unsigned long index
 );
Parameters
type
The type of data to add.
data
The data to add.
index
The index of the data to add.

mozTypesAt()

Holds a list of the format types of the data that is stored for an item at the specified index. If the index is not in the range from 0 to the number of items minus one, an empty string list is returned.

Note: This method is Gecko-specific.
 nsIVariant mozTypesAt(
   in unsigned long index
 );
Parameters
index
The index of the data for which to retrieve the types.

See also

Drag and Drop

Document Tags and Contributors

Last updated by: brunoais,