Javascript Move opener window

有些话、适合烂在心里 提交于 2021-01-07 06:04:25

问题


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 };
}

回答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



来源:https://stackoverflow.com/questions/14632143/javascript-move-opener-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!