views:

94

answers:

1

Hello!

I want to display picture in a new window. For that I wrote javascript function that opens new window. I open 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 FF and Chrome all works.

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 hot make it work?

A: 

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();
    }
};
Tim Down