Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Stop a user from leaving the page?

Hi how do I stop a user from leaving the page and showing a dialog to say if they want to continue leaving the page?
0
eNarc
Asked:
eNarc
  • 3
  • 2
4 Solutions
 
cmalakarCommented:
Use window.onbeforeunload event.

window.onbeforeunload = askUser;
function askUser()
{
   alert(".....");
}
0
 
eNarcAuthor Commented:
how do I get a dialog with ok to leave, cancel to stay?
0
 
mwochnickCommented:
here's an article explaining how the onbeforeunload event works
http://www.webreference.com/dhtml/diner/beforeunload/bunload4.html

function are_you_sure()
{
input_box=confirm("Click OK or Cancel to Continue");
if (input_box==true)

{ 
// Output when OK is clicked
alert ("You clicked OK"); 
}

else
{
// Output when Cancel is clicked
alert ("You clicked cancel");
}

}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mwochnickCommented:
just to be clear - the code example above is just a confirm dialog you still need to tie it to the onbeforeunload event
0
 
cmalakarCommented:
You just need the below code.. browser will take care of rest.


window.onbeforeunload = askUser;
function askUser()
{
   return "Want to leave this page ?"
 }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now