pioSko
pioSko

Reputation: 551

jQuery window/popup function executing too early

I'm sorry for posting a "semi-duplicate", but I've stopped getting answers in another question because it seems answered.

Question related to: jQuery target different window/popup.

So, i have this function which when fired creates a new window/popup. The problem is that jQuery is supposed to be listening for when the new popup is closed and do an alert, but the alert is fire as soon as the popup is created.

the code:

function idealPopUp(url){
    var windowName = "idealPopUpWindow";
    var windowSize = 'height=820,width=704,toolbar=no,scrollbars=yes';

    var idealPopUpWindow = window.open(url, windowName, windowSize);

    $(idealPopUpWindow).unload( function () {
        alert("BING");
    });
    event.preventDefault();
}

Upvotes: 1

Views: 2020

Answers (1)

Rob W
Rob W

Reputation: 348992

Bind the event after the page has loaded:

function idealPopUp(url){
    var windowName = "idealPopUpWindow";
    var windowSize = 'height=820,width=704,toolbar=no,scrollbars=yes';

    // Wrap new window object in a jQuery object
    var $idealPopUpWindow = $(window.open(url, windowName, windowSize));

    $idealPopUpWindow.load( function () {     //  Execute this function on load
        $idealPopUpWindow.unload(function(){  // Bind the actual event
            alert("BING");
        });
    });
}

Upvotes: 3

Related Questions