nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

How to disable drag and drop?

Open allencblee opened this issue 14 years ago • 11 comments

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?

allencblee avatar Nov 27 '12 23:11 allencblee

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.

zcbenz avatar Nov 28 '12 00:11 zcbenz

Tried it already but neither dragover, dragend, ondrop events are fired. I've also tried to set the events for body and iframe.

allencblee avatar Nov 28 '12 02:11 allencblee

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:

  1. We can only use addEventListener for document.body.
  2. To listen to drop we must also listen to dragover.

zcbenz avatar Nov 28 '12 02:11 zcbenz

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 avatar Nov 28 '12 05:11 allencblee

@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.

eachcan avatar Nov 06 '14 12:11 eachcan

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.

felixSchl avatar Dec 02 '14 00:12 felixSchl

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?

stephan-nordnes-eriksen avatar Jun 04 '15 08:06 stephan-nordnes-eriksen

Yeah.. It was me messing it up

stephan-nordnes-eriksen avatar Jun 04 '15 09:06 stephan-nordnes-eriksen

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?

danschumann avatar Oct 21 '16 00:10 danschumann

I am haveing the same trouble with you @felixSchl, did you fixed it?

rellame avatar Dec 09 '16 03:12 rellame

you can try adding ondragstart="return false;" ondrop="return false;" in the html tag to disable the drag & drop

sayaniHalderMphasis avatar Feb 10 '21 13:02 sayaniHalderMphasis