onMousedown and drag, resize Iframe simultaneously

Posted on 2003-10-31
Last Modified: 2012-08-14
This code works well for dragging and resizing a div.  I have an iframe inside of a div and I'm having trouble getting it to work..  

There's a couple DIVs defined inside there.. I added the moveable = true to mine and I get 0 for my element when I click on my iframe.  


  DIV {cursor: hand}

<body style="font-family: verdana">
<H2>Simple Drag and Resize Example</h2>
<h4>Use alt-click to multi-select</h4>

<input type=button value="Moving, click to resize"
  onclick="toggleMoveResize(this); return false">

<div moveable=true style="position: absolute;
                   top: 150px; left: 100px;
                   width: 100px; height: 100px;
                   background-color: red;
                     border: solid 2px black">
Click and drag me

<div moveable=true style="position: absolute;
                   top: 150px; left: 250px;
                   width: 100px; height: 100px;
                   background-color: blue;
                     border: solid 2px black">
Click and drag me

<script language="JavaScript">
var activeElements = new Array();
var activeElementCount = 0;

var lTop, lLeft;
var doMove = true;
var doResize = false;

function toggleMoveResize(e) {
  if (doMove) {
    doMove = false;
    doResize = true;
    e.value = "Resizing, Click to Move";
  } else {
    doMove = true;
    doResize = false;
    e.value = "Moving, Click to Resize";

function mousedown() {
  var mp;

  mp = findMoveable(window.event.srcElement);

  if (!window.event.altKey) {
     for (i=0; i<activeElementCount; i++) {
        if (activeElements[i] != mp) {
          activeElements[i].style.borderWidth = "2px";
     if (mp) {
       activeElements[0] = mp;
       activeElementCount = 1; = "4px";
     } else {
       activeElementCount = 0;
  } else {
     if (mp) {
       if ( != "4px") {
         activeElements[activeElementCount] = mp;
         activeElementCount++; = "4px";

  window.status = activeElementCount;

  lLeft = window.event.x;
  lTop = window.event.y;

document.onmousedown = mousedown;

function mousemove() {
  var i, dLeft, dTop;

  if (window.event.button == 1) {

    sx = window.event.x;
    sy = window.event.y;

    dLeft = sx - lLeft;
    dTop = sy - lTop;

    for (i=0; i<activeElementCount; i++) {
      if (doMove)
        moveElement(activeElements[i], dLeft, dTop);
      if (doResize)
        resizeElement(activeElements[i], dLeft, dTop);

    lLeft = sx;
    lTop = sy;

    return false;


function moveElement(mp, dLeft, dTop) {
    var e
    e = mp; += dTop; += dLeft;

function resizeElement(mp, dLeft, dTop) {
    var e;
    e = mp; += dTop; += dLeft;

function findMoveable(e) {
  if (e.moveable == 'true')
    return e;

  if (e.tagName == "BODY")
    return null;

  return findMoveable(e.parentElement);

function findDefinedMoveable(e) {
  if ((e.moveable == 'true') || (e.moveable == 'false'))
    return e;

  if (e.tagName == "BODY")
    return null;

  return findDefinedMoveable(e.parentElement);

function rfalse() {
  return false;

document.onmousemove = mousemove;
document.onselectstart = rfalse;





function iframeOpen ( iframeUrl, iframeId ) {          
    parentSelected = true;
    var iframe = document.createElement('iframe');
      var divId = "d"+iframeId;
    iframe.outerHTML = "<div id='"+divId+"' moveable='true' STYLE='position:absolute; top:80; z-index:999; left:150;'> <iframe WIDTH=100% HEIGHT=100% src='"+iframeUrl+"' name='"+iframeId+"' id='"+iframeId+"' allowtransparency frameborder='0' scrolling='no' STYLE='position:absolute; display:block;'></iframe></div>";
    if(!WindowOpened) {
    WindowOpened = true;
    TheFrameOpen = document.frames[iframeId];
    TheWindowOpen = document.getElementById(iframeId);    
    WaitForClose ();
    return TheWindowOpen;
  function iframeClose( iframeId ) {                 
Question by:dgelinas
LVL 10

Accepted Solution

NetGroove earned 250 total points
ID: 9658834
I wonder you even get an event from the IFRAME!
Iframes are separate windows and separate document context.
You can not capture events from iframe in parent document.

LVL 23

Assisted Solution

sciwriter earned 250 total points
ID: 9661954
Depending on the browser, IFrame will/will not work -- remember, it is a Netscape feature initially.

You've asked this same question many ways so far, and the answer is that it will NOT resize in one or more browsers.  Therefore you have 2 choices to solve the problem --

1.  Don't use an IFrame if you want cross-browser compatibility, as it is not reliably resizable.  Instead, use a DIV alone, not an IFrame, or better still, is REAL FRAMES,  and decare it resizable -- that will work in ALL browsers that support frames.  -- or --

2.  Rewrite the entire page wit document.write, in which case you will have to provide anchors at the corner of the IFrame, and onMouseUp, you will have to rewrite the page.

These are the only solutions I know about.  Consider rethinking your page, and don't beat the IFrame to death -- as I recall, it is poorly supported in MSIE, and that is most of your audience, sadly .....
LVL 16

Expert Comment

ID: 10346457
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: NetGroove {http:#9658834} & sciwriter {http:#9661954}

Please leave any comments here within the next four days.

EE Cleanup Volunteer

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question