3

I have now tested quite a few things, and I understand it's not optimal, but I need drag and drop from main page to an iframe. Both are on the same domain.

I have tested with iframeFix, but either it's not supported in my browser (Chrome) or I do something wrong.

http://api.jqueryui.com/draggable/#option-iframeFix

<div id="mycontainer" class="mycontainer">
    <iframe id="iframewindow" src="./child.html" frameborder="0" width="100%" height="100%"></iframe>
</div>

in the iframe:

<div id="sortable">
  <div class="portlet">
     some content
  </div>
</div>

(I use jQuery UI inside the iframe for sortable.)

The script for loading draggable inside the main page:

$(function() {

   $( ".draggable" ).draggable({
      connectToSortable: "#sortable",
      helper: "clone",
      iframeFix: true,
      helper: function(event) {
            return "<div class='custom-helper'>I move this</div>";

      },
      revert: "invalid",
    });
   $().disableSelection();
)};

I have tested with making overlays etc, but somehow I haven't got it to work.

Is it a way that makes drag and drop from a html page to an iframe work? (In all browsers?)

I do not need jQuery UI draggable if another solution works well.

4
  • To confirm, you're attempting to drag an element from a page, to a sortable, on a page within an iFrame?
    – Twisty
    Commented Dec 15, 2016 at 23:05
  • Yes. My list of draggable items (elements) are inside the main HTML, and I drag them over and into a sortable within an iframe.
    – Preben
    Commented Dec 15, 2016 at 23:19
  • Unfortunately I can not make a jsfiddle for this case, else I would have done so.
    – Preben
    Commented Dec 15, 2016 at 23:20
  • Started a fiddle: jsfiddle.net/Twisty/gkxe8vpy
    – Twisty
    Commented Dec 16, 2016 at 0:13

1 Answer 1

5

I found this answer and was able to apply it: jQuery-ui droppable to sortable inside iframe

Here is my Fiddle: https://jsfiddle.net/Twisty/gkxe8vpy/4/

HTML

<div id="mycontainer" class="mycontainer">
  <iframe id="iframewindow" src="" frameborder="0" width="100%" height="100%"></iframe>
</div>

<div id="draggable" class="ui-state-highlight">Drag Me</div>

JavaScript

/**
 * Code to populate iFrame, mimics actual SRC
 */
var frameHTML = "<div id='sortable'><div class='portlet'>some content</div></div>";

var $iframe = $("#iframewindow");
$iframe.ready(function() {
  $iframe.contents().find("body").html(frameHTML);
});
/**
 * End of iFrame code
 */
$(function() {
  $("#draggable").draggable({
    connectToSortable: $iframe.contents().find("#sortable").sortable({
      items: "> div",
      revert: true,
    }),
    helper: "clone",
    iframeFix: true,
    helper: function(event) {
      return "<div class='custom-helper'>I move this</div>";
    },
    revert: "invalid"
  });
  $iframe.contents().find("#sortable").disableSelection();
});

The "trick" here is to create the sortable as a target of the connectToSortable option. This returns a selector as needed and the 2 object will be aware of each other.

Note that your iframe should be just plain HTML (do not initialize sortable there or it will misbehave)

4
  • I actually still don't get it to work with "real" iframe. The example works well, and I have played around with the jsfiddle. However when trying it on my local computer with a real iframe it does not work. This issue with iframes is probably too sensitive / vulnerable in jQuery. Might just test this more: blog.stackhive.com/post/137799349684/… (that seams rock solid compared to jQuery)
    – Preben
    Commented Dec 16, 2016 at 12:14
  • 1
    @Preben there are a lot of contributing factors and jQuery is potentially one of them. Most trying to protect from XSS, so I would test strongly in all common browsers to ensure the solution works.
    – Twisty
    Commented Dec 16, 2016 at 16:19
  • Yes, I am looking into this. I might, using a hack, be able to add the draggable into the iframe itself. This way I am certain it will work. :-) Thank you!
    – Preben
    Commented Dec 16, 2016 at 20:05
  • I was considering that, something like making the parent div droppable, and that triggering the creation of a sortable item in the child page.
    – Twisty
    Commented Dec 16, 2016 at 22:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.