Earth
Earth

Reputation: 3571

Is there any way to prevent the browser closing after clicking on the "Close" button

My requirement is as follows:

When the User clicks the close button on the browser in a page, the customized popup window will appear. I had designed the popup window and I can open that window with the following code:

 window.onbeforeunload = function (e) {
            openwindow(200, 100, 'Close.aspx');
        };

But my ultimate requirement is that the main parent window should not be closed after clicking any buttons on the popup window. I don't want the browser's default alert return message window and I am displaying the popup window here instead of browser's default alert window. Is there way to stop the browser closing..?

Upvotes: 1

Views: 3817

Answers (1)

sajanyamaha
sajanyamaha

Reputation: 3198

Due to security reasons this is not possible!

You can show a confirmation dialog using onbeforeunload (see e.g. here for how to do it), giving the user the choice to not leave the page after all. But you can't prevent the closing against the user's will.

window.onbeforeunload = function(e) {
    return 'Dialog text here.';
};

Upvotes: 5

Related Questions