Search code examples
javascriptjqueryjquery-uijquery-ui-draggablejquery-ui-droppable

Drag an item from parent frame and drop it in a droppable object in child iframe


I am trying to drag an item from the parent frame into a droppable element but in an iframe. It works when dropping the item on a droppable element in the same frame but when trying it across frames, it does nothing.

I have tried to work around this a lot of times but with no success.

Here's the code to the parent frame:

<html>
<head>
    <title></title>
    <link rel="stylesheet"                             href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<style>
  #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  });
  </script>
</head>
<body>
    <div id="draggable" class="ui-widget-content">
  <p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>
<iframe src="P1.html" width = "500px" height = "500px">
</iframe>

</body>
</html>

And Here's the code to the iframe:

<html>
<head>
    <title></title>
    <link rel="stylesheet"                             href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<style>
  #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  });
  </script>
</head>
<body>
    <div id="draggable" class="ui-widget-content">
  <p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>    
</body>
</html>

Solution

  • Unfortunately, frames are separate window objects, so you won't be able to drag an object from one DOM tree into another window's DOM tree. A webpage renders based on the current state of the window's DOM. As you drag your element around, The DOM signals a repaint and the webpage re-renders based on the style of your element (in the case of dragging, the top and left style properties are usually the culprit).

    When you have two frames (or window objects) on your page (one inside the other in this scenario), you also have two completely separate instances of this DOM object and, thusly, it's behaviour. DOM's don't collaborate when one is inside the other and perform one larger DOM render together; they're entirely different scopes (including JavaScript) and it's designed this way for a reason (mostly for security as you can load external websites into embedded window/iFrame objects).