Aleksandr Ivanov
Aleksandr Ivanov

Reputation: 2786

window.attachEvent to resize a window in IE

I want to display picture in a new window. For that I wrote JavaScript function that opens new window. I open a window with some fixed size (for example 500 x 500), but I want to resize my window to picture size. For that I attach event to this newly created window. Here is only code for IE because in Firefox and Chrome all work.

var win = null;
function showPicture(url) {
    win = window.open(url, "pic", "height=500,width=500");
    if (window.attachEvent) {
        win.attachEvent("onload", correctWinSize);  
    }
    // Not IE attach event code
}

function correctWinSize() {
     // Here is resize code
}

Problem is that event fires only sometimes. I just click on hyperlink that calls showPicture() and window is sometimes resized and sometimes not. Have you any ideas how to make it work?

Upvotes: 2

Views: 3342

Answers (1)

Tim Down
Tim Down

Reputation: 324627

You can't capture the load event of another window reliably cross-browser. You need to handle the load event in the pop-up window and call a function on the main window to tell it it's loaded:

In the main window

function popUpLoaded() {
    alert("Loaded!);
    // Do your resizing thing
}

In the pop-up

window.onload = function() {
    // Check the opener still exists to prevent an error in the
    // event it's been closed
    if (!window.opener.closed) {
        window.opener.popUpLoaded();
    }
};

Upvotes: 1

Related Questions