How to disable drag and drop?
I've written an application with node-webkit. But when I drag a html file into the canvas, the html is displayed replacing my app. Is there any way to prevent this?
You can listen to HTML5's ondrop event and just ignore it, you can try code in Dragging files into page.
And please use our mailing list for this kind of quetions.
Tried it already but neither dragover, dragend, ondrop events are fired. I've also tried to set the events for body and iframe.
Try this:
document.body.addEventListener('dragover', function(e){
e.preventDefault();
e.stopPropagation();
}, false);
document.body.addEventListener('drop', function(e){
e.preventDefault();
e.stopPropagation();
}, false);
There are some quirks for this:
- We can only use
addEventListenerfordocument.body. - To listen to
dropwe must also listen todragover.
I found that we need to set the event handler on document rather than document.body. In nay case, it works now. Thanks a lot.
@allencblee It's happened in my app as yours. It does not work fine. I can not prevent the actions.
I tried to bind drop and dragover events to document and document.body, but it also NOT work fine.
I cannot get this to work by any means on an iframe. If I drag an image over an iframe, it will just replace my whole app with that image.
I am having trouble with this still.. I did, I believe, was is described in drag files into page. Is this a bug, or am I failing at writing window.ondragover = function(e) { e.preventDefault(); return false }; window.ondrop = function(e) { e.preventDefault(); return false }; at the top of my script?
Yeah.. It was me messing it up
This is behaving RANDOMLY. Sometimes 1st, 2nd, 3rd, but eventually I will drag a file and it will not catch the event. Then my app is basically busted because its on another url and can't go back..
Okay, after banging my head and trying EVERYTHING..
This was breaking (coffeescript)
$(window).on('dragover dragenter', (e) =>
e.preventDefault()
#$('#editorIndex').css opacity: .5
@setDrag(true)
console.log true
)
$(window).on('dragleave dragend', (e) =>
e.preventDefault()
#$('#editorIndex').css opacity: 1
@setDrag(false)
console.log false
)
The setDrag true / false was firing repeatedly back to back. Perhaps because setDrag creates a new element to overlay the whole page (position fixed).
When only the opacity was changing, it was fine, but calling setDrag would sometimes, sometimes not break it. It was very very random. ( by break it I mean the page would not catch on 'drag' events on any element. I set about 50 elements to all catch it. (using jquery and without, html, body, document.body, specific elements, etc)
Eventually I went NUCLEAR... and just created a <input type="file" style="visibility: hidden; position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 999999" />
So that no matter what, no matter what, it won't navigate my page. You can always drop a file into a file input.
$('#main_page).on('dragover dragenter', (e) =>
//popup fixed element
//do stuff
)
$('#fixed_element').on('dragleave dragend', (e) =>
// close element
//undo stuff
)
I'm still using my nuclear navigation deterrant, however the above code seems to not flip/flip repeatedly. I have a feeling even though the drag doesn't end on the window, perhaps it gets the event propagated from an inner dom element?
Still, if I have a listener on the window, it should always catch and preventDefault for drag, right? Not randomly fail based on how much rendering is going on, or how many other drag events are getting fired?
I am haveing the same trouble with you @felixSchl, did you fixed it?
you can try adding ondragstart="return false;" ondrop="return false;" in the html tag to disable the drag & drop