HassanF
HassanF

Reputation: 455

How can I close a tab of browser after getting confirmation from user?

I want to close a tab after getting confirmation from user. It can be done as follow-

window.onbeforeunload = function() {
       return "Are you sure?";
}

But the problem with this code is that it shows some message that I am not expecting. I want to do this task using javascript's confirm() popup. can anyone help me to do this.

Thanks in advance.

Upvotes: 0

Views: 247

Answers (2)

Tim Lamballais
Tim Lamballais

Reputation: 1054

function closewindow() {
    if (confirm("Are you sure?")) {
        window.close();
    }
}

Upvotes: 0

Adam Plocher
Adam Plocher

Reputation: 14243

This is really the only way to handle this. The software companies that created these browsers went to great lengths to make sure this could not be abused, and this is the solution they came up with. You can't trigger any custom javascript beyond providing a string that the browser handles, when the user closes the browser.

Upvotes: 2

Related Questions