0

I am trying to implement drag and drop in iframe in Spring (working in Eclipse 2020-03) and iframe just does not seem to accept any possibility of cooperation. I came across this - arguably older - [snippet of working code][1] but the iframe in eclipse is not even able to accept window.onload. Where is the problem?

EDIT: adding my code

<script>
    var dragged;

    /* events fired on the draggable target */
    document.addEventListener("drag", function(event) {

    }, false);

    document.addEventListener("dragstart", function(event) {
        dragged = event.target;
    }, false);

    document.addEventListener("dragover", function(event) {
        event.preventDefault();
    }, false);

    document.addEventListener("drop", function(event) {
        event.preventDefault();
        if (event.target.className == "dropzone") {
            dragged.parentNode.removeChild(dragged);
            event.target.appendChild(dragged);
        }
    }, false);
</script>

html:

<div id="draggable" draggable="true"
        ondragstart="event.dataTransfer.setData('text/plain',null)">
        Drag me </div>

<div class="dropzone">Test dropzone</div>

<iframe class="dropzone">Test iframe dropzone</iframe>
krenkz
  • 484
  • 6
  • 15
  • 1
    The code you posted is from the supposedly working snippet (which isn't working at all for me). Perhaps provide your own faulty code instead? – Fullslack Jul 27 '20 at 06:33
  • Being a newbie I just copied and slightly adjusted the code from [here](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API) – krenkz Jul 27 '20 at 14:42
  • [JSFiddle](https://jsfiddle.net/ro9pbjcd/) for the updated code provided – Fullslack Jul 27 '20 at 14:49
  • interesting...this is not working for me (and I checked in firefox, vivaldi and from another computer in chrome) – krenkz Jul 27 '20 at 15:03
  • Sorry the JSFiddle is only for the code you gave in the update. Still working on making a example that works. – Fullslack Jul 27 '20 at 15:07
  • I probably should have mentioned that when dragging over iframe, the symbol changes to the 'forbidden' icon like [here](https://stackoverflow.com/questions/52196824/javascript-event-listeners-not-firing-for-drag-and-drop) - I already disabled my addblock and restarted everything, but it is not working – krenkz Jul 27 '20 at 15:49

1 Answers1

1

Using the code from this example (with jQuery), I came up with the following (although very crude) solution:

<div id="draggable" draggable="true"
        ondragstart="event.dataTransfer.setData('text/plain',null)">
        Drag me </div>

<div class="dropzone">Test dropzone</div>

<iframe id="dropframe" srcdoc='<div class="dropzone2">Test iframe dropzone</div>'></iframe>
    var dragged;
    //var dragged2;
    var iFrame = document.getElementById("dropframe");
    var iFrameWindow = iFrame.contentWindow;
    
    iFrame.addEventListener("load", function(event) {
        iFrameWindow.document.body.addEventListener('dragstart', function(event) {
        event.preventDefault();
      });
        iFrameWindow.document.body.addEventListener('dragenter', function(event) {
        event.preventDefault();
        //dragged2 = event.target;
      });
        iFrameWindow.document.body.addEventListener('dragover', function(event) {
        event.preventDefault();
      });
        iFrameWindow.document.body.addEventListener('drop', function(event) {
        event.preventDefault();
        //console.log(dragged2);
        if (event.target.className == "dropzone2") {
            console.log('drop event');
            dragged.parentNode.removeChild(dragged);
            event.target.appendChild(dragged);
        }
      });
    });

    /* events fired on the draggable target */
    document.addEventListener("drag", function(event) {

    }, false);

    document.addEventListener("dragstart", function(event) {
        dragged = event.target;
    }, false);

    document.addEventListener("dragover", function(event) {
        event.preventDefault();
    }, false);

    document.addEventListener("drop", function(event) {
        event.preventDefault();
        if (event.target.className == "dropzone") {
            dragged.parentNode.removeChild(dragged);
            event.target.appendChild(dragged);
        }
    }, false);

A JSFiddle is provided as well.

This is what I think you wanted. Is it working perfectly? No, for that I would suggest looking at the jQuery solution (which is way more extensive than my little demo).

Edit:

Made a new JSFiddle with a increased height and width on the div inside the iframe. This allows to drop anywhere instead of only on the text.

Edit 2:

For some reason the condition if (event.target.className == "dropzone2") did not work for me in eclipse; when removed, everything works fine

krenkz
  • 484
  • 6
  • 15
Fullslack
  • 290
  • 1
  • 2
  • 11
  • Hmmm, it displays the text 'Test iframe dropzone' (something my code did not manage) but still refuses to accept the dragged item. I will definitely look at that jQuery. Thank you! – krenkz Jul 27 '20 at 16:25
  • 1
    Did you drop it directly on the text (_Test iframe dropzone_)? If not it doesn't work, because the _body_ has the listener and is only made up out of the text. Also the _iframe_ text source is in the `srcdoc tag`, or for a file the `src tag`. – Fullslack Jul 27 '20 at 16:53
  • omg, it didnt cross my mind...yes,it works perfectly. Thank you! – krenkz Jul 27 '20 at 17:01
  • The iframe in Eclipse will not even display the 'Test iframe dropzone' text – krenkz Jul 27 '20 at 17:16
  • Strange. Is it running directly in Eclipse or starting a Tomcat server instance and opening into a browser on your machine (ie: Chrome, Firefox, Edge, Safari)? Also updated the answer with a bigger drop area on the iframe. **Edit:** It might be caused because of my _"wonderful"_ inline code, try making a separate file from the _div_ and load that using the `src=("file")` tag. – Fullslack Jul 27 '20 at 17:16
  • it starts a tomcat server and opens vivaldi or firefox (I checked both of them, no change) – krenkz Jul 27 '20 at 17:20
  • just used chrome - no change (so I dont think its the browser settings) – krenkz Jul 27 '20 at 17:33
  • Really, really strange behavior. I just tested the code in a empty .html file and it works fine in both Chrome and Firefox. How do you inject the code in Eclipse (source would be helpful)? – Fullslack Jul 27 '20 at 20:38
  • Would you be so kind and have a look at [this github repo](https://github.com/grwkremilek/iframe) with my original and your codes? – krenkz Jul 27 '20 at 21:00
  • 1
    Move the `` code block to the bottom of your `` and it works (below all the _div_, just before the ``). JavaScript should be loaded last in your DOM, so you want it to be on the bottom always. Placement in __ is not the way anymore. – Fullslack Jul 27 '20 at 21:09
  • It started to behave differently! The iframe area now recognizes dragover (I added a listener to change colour on dragenter and the iframe indeed changes to purple) but it refuses to drop! – krenkz Jul 27 '20 at 21:40