0

I have two windows; one is inside a frameset and the other is opened from the other in a new window (window.open(...)).

I am trying to move the left edge of the opened window to the right edge of the opener window.

Here are the requirements:

  1. Must work in IE8.
  2. Cannot modify the code in the opener window.
  3. The opened window should be completely visible (See the X in the upper-right hand corner)

I was able to find a solution that gets the correct width and height (with the scroll bars) of a IE8 window. It involves moving the window and checking it's value, but when I run window.opener.moveTo() it moves the frame instead of the actual window and thus doesn't record the correct values.

Here is the code I am currently using:

function moveAway()
{
    //Array Style
    var main = getWindowSize(window.opener)
    var child = getWindowSize(window)

    //How to move
    var topAvail  = main.top;
    var bottomAvail = window.opener.screen.availHeight - main.bottom;
    var leftAvail = main.left;
    var rightAvail = window.opener.screen.availWidth - main.right;

    var choice = Math.max(topAvail,bottomAvail,leftAvail,rightAvail)

    if(choice == rightAvail)
    {
        window.moveTo(main.right,main.top)
        window.resizeTo(rightAvail,main.bottom)
    }else if(choice == bottomAvail)
    {
        window.moveTo(main.left,main.bottom)
        window.resizeTo(window.opener.document.body.clientWidth,bottomAvail-36)
    } else if(choice == leftAvail)
    {
        window.moveTo(0,main.top)
        window.resizeTo(leftAvail,main.bottom)
    } else if(choice == topAvail)
    {
        window.moveTo(main.left,0)
        window.resizeTo(main.right,topAvail)
    }
    //return "item\ttop\tleft\tbottom\tright\nmain\t" + main.join("\t") + "\nchild\t" + child.join("\t")
}


function getWindowSize(windowObj) {
    var wW, wH;
    var wT = windowObj.screenTop;
    var wL = windowObj.screenLeft;
    if (windowObj.outerWidth) {
        wW = windowObj.outerWidth;
        wH = windowObj.outerHeight;
    } else {
        var cW = windowObj.document.body.offsetWidth;
        var cH = windowObj.document.body.offsetHeight;
        windowObj.resizeTo(500,500);
        var barsW = 500 - windowObj.document.body.offsetWidth;
        var barsH = 500 - windowObj.document.body.offsetHeight;
        wW = barsW + cW;
        wH = barsH + cH;
        windowObj.resizeTo(wW,wH);
    }
    return { right: wW, bottom: wH, top : wT, left : wL };
}
milesacul
  • 145
  • 4
  • 13
  • A window inside of a frameset? You'd need to figure out where that window is in relationship to the parent of the frameset (the one that is an actual window object), but maybe you don't because you're only aligning it to the right. If you could create a reduction at http://jsfiddle.net, it'd be easier for people to analyze what's not working and visualize the relationship between frames, framesets and popups. – Ruan Mendes Jan 31 '13 at 18:06
  • Framesets and windows in frames in IE8, what kind of sourcery is this? I'm not sure what's the real target to move, if it's the `frameset` window, you can try `window.opener.top`. If not, can you add a codesnippet of the `frameset`s and mark all parents of opened windows there? – Teemu Jan 31 '13 at 18:18
  • What if the current window is already taking up the whole screen? You're out of luck in that case – Ruan Mendes Jan 31 '13 at 18:47
  • The system is set up so that the window won't take up the whole screen. – milesacul Jan 31 '13 at 19:09

1 Answers1

1

How do I get JavaScript to open a popup window on the current monitor deals with positioning popup windows and contains a lot of insight into getting screen and window coordinates and positioning windows

You must beware that many browsers will disallow funny behavior like

  • Off-screen windows
  • windows wider than a screen
  • windows that span multiple screens

With code from my answer on the question mentioned above, you can get started with the following

// Pops a window relative to the current window position
function popup(url, winName, xOffset, yOffset, width, height) {
    width = width || 100;
    height = height || 100;  
    var x = (window.screenX || window.screenLeft || 0) + (xOffset || 0);
    var y = (window.screenY || window.screenTop || 0) + (yOffset || 0);
    return window.open(url, winName, 'top=' +y+ ',left=' +x + ',width=' + width + ',height='+ height);
}

var size = getWindowSize(window);
var popupWin = popup("http://www.google.com", "popup", size.right -  size.left);

I'm not addressing your concern that the popup window must all be on screen. That is because I don't know what you could possibly do if the main window is full screen.

Pop windows are passé, I know some people want to use it, but the browsers are really fighting against it (by imposing rules that came out of tabbed windows environments). You're swimming against the current here.

Last point, you shouldn't need to move your window, you can just specify top/left and width/height, instead of having a window that moves around after it has been displayed. See my popup function.

Here's a jsfiddle to get you started showing us your problem

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • This would violate requirement 2: Cannot modify the code in the opener window. The window in the frame could be one of 30+ pages and the popup window is a jsp page that pulls information from the database that helps the user make a decision. – milesacul Jan 31 '13 at 19:22
  • @user1741454 I see, but I can't help you anymore unless you create a reduction of your problem – Ruan Mendes Jan 31 '13 at 19:59