Skip R.
Skip R.

Reputation: 459

confirmation before exit dialog

In most pages, if your doing an action, (like editing, creating), and when I attempt to exit, it mostly prompts me if I really want to exit. The dialog gives me two options: leave or cancel and continue. How do you accomplish this in JavaScript? Do I have to use a meta element? And please don't mention beforeunload, unless it's the true and only way to accomplish this.

Upvotes: 12

Views: 26219

Answers (3)

Eos Antigen
Eos Antigen

Reputation: 388

what I use is:

onClick="return confirm('Sure you want to exit..?')"

Upvotes: 0

Alex Ruhl
Alex Ruhl

Reputation: 459

window.onbeforeunload = function() { 
      if(confirm('are you sure to exit?')) 
           return true; 
      else 
           return false; 
};

Upvotes: -5

casraf
casraf

Reputation: 21694

Why not mention onbeforeunload? It's the built in way to do so, and I don't see a problem using it.

function myConfirmation() {
    return 'Are you sure you want to quit?';
}

window.onbeforeunload = myConfirmation;

Upvotes: 21

Related Questions