The ondrop attribute is a global event handler that runs JavaScript when the drop event fires on a drop target — when a dragged item or text selection is released on that element.
To make drop fire, call event.preventDefault() in ondragover on the same target. In drop, call preventDefault() again to stop the browser’s default (e.g. opening a link). Pair a draggable source with ondragstart (setData) and read it here with getData.
Baseline · Widely available.
Not deprecated and not experimental. Stable since about July 2015. Per MDN, drop is cancelable and may bubble — and you should always allow it via dragover + preventDefault().
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer element.addEventListener("drop", handler) in production; use inline ondrop for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<element ondrop="script">
script is JavaScript run when a drop lands. Prefer: element.addEventListener("drop", handler).
Values
Attribute Values
The attribute value is JavaScript. Common patterns:
Mismatch of types returns an empty string — keep names identical.
Example 3 — Drop Files from the Desktop
OS file drops use dataTransfer.files (no setData needed).
HTML
<div id="zone">Drop files here</div>
<p id="msg"></p>
<script>
var zone = document.getElementById("zone");
zone.addEventListener("dragover", function (e) {
e.preventDefault();
});
zone.addEventListener("drop", function (e) {
e.preventDefault();
var files = e.dataTransfer.files;
if (!files.length) {
document.getElementById("msg").textContent = "No files in this drop.";
return;
}
var names = [];
for (var i = 0; i < files.length; i++) {
names.push(files[i].name);
}
document.getElementById("msg").textContent = "Files: " + names.join(", ");
});
</script>
No source element — the OS supplies the drag payload.
dragover + preventDefault still required for the zone.
files is a FileList on dataTransfer.
Validate on the server if you upload — never trust client-only checks.
Edge Cases & Pitfalls
If drop never fires, check dragover first — not your drop handler.
dragover
Must allow drop in dragover
Without preventDefault() on dragover, the browser refuses the drop and drop never runs.
default
Call preventDefault in drop too
Stops navigation or opening a dropped URL/link as a page load.
types
setData / getData must match
Use the same MIME string (e.g. text/plain) on both ends or you get an empty string.
a11y
Offer a non-drag path
Provide buttons or file inputs for the same move/upload action. Announce results with aria-live.
files
Validate uploads server-side
Client file drops are UX only. Check type, size, and content on the server before saving.
Compatibility
Browser Support
ondrop is an HTML event handler attribute for the browser Drag and Drop API. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
ondrop
Widely available since about July 2015. Desktop Chrome, Firefox, Safari, and Edge support it well. File-drop UX and touch drag still differ by platform.
96%Global support (approx.)
Google Chrome4+
Full support
Mozilla Firefox3.5+
Full support
Apple Safari3.1+
Full support
Microsoft Edge12+
Full support
Opera12+
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
ondropWidely supported
Bottom line: Safe to use for drop targets on modern sites. Prefer addEventListener, always pair with dragover + preventDefault, and validate file drops on the server.
Remember
Key Takeaways
Role: runs on the target when a valid drop lands — move nodes or read data.
Requiresdragover + preventDefault() or drop never fires.
getData / files: read text payloads or OS file drops here.
Also preventDefault in drop to block browser navigation defaults.
One line:ondrop handles the landing — unlock it with dragover + preventDefault().
Frequently Asked Questions
It is an inline event handler that runs JavaScript when the drop event fires on a drop target — when a dragged item or text selection is released on that element.
Browsers block drops by default. Call event.preventDefault() in the dragover handler on the same target so drop can fire. MDN recommends always doing this.
Yes, usually. It stops the browser’s default action for the drop (for example, navigating when a link or URL is dropped).
Use event.dataTransfer.getData("text/plain") (or another MIME type) matching what you stored with setData in dragstart. For OS files, use event.dataTransfer.files.
Prefer element.addEventListener("drop", handler). Inline ondrop works for learning. MDN discourages HTML event handler attributes in production.
ondrop fires on the drop target when a valid drop happens. ondragend fires on the source when the drag finishes, whether or not a drop succeeded.