[Webinar] Streamline your web hosting managementRegister Today

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

Is there a way to interupt page unload using javascript confirm msgbox?

When a user clicks the back button I would like a confirm msgbox to ask "are you sure" if ok is clicked then the page unloads and the previous page is loaded, if cancel is clicked the page doesn't unload. is this possible?

thanks

Rick
0
rdowdall
Asked:
rdowdall
2 Solutions
 
fritz_the_blankCommented:
You could try using the confirm method on the onUnLoad event:

if (confirm("Are you sure you want to leave) == true){
  return true;
}else{
   return false;
}

Fritz the Blank
0
 
fritz_the_blankCommented:
Oops, I forgot to close the quotation marks:

if (confirm("Are you sure you want to leave?") == true){
 return true;
}else{
  return false;
}

Fritz the Blank
0
 
DeAnCommented:
something like this?

<html>
<head>
<title></title>
<script language="JavaScript">
<!--
function ask() {
      if (!confirm("Are you sure?")){
          location.href=location.href
     }
}
//-->
</script>
</head>
<body onUnload="ask()">


</body>
</html>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
fritz_the_blankCommented:
That didn't work. This sort of works:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function noLeave(){
     if (confirm("Are you sure you want to leave?") == true){
          return true;
     }else{
           location.replace(location.href) ;
     }
}
//-->
</SCRIPT>


</HEAD>
<BODY onUnLoad="noLeave()">

<P>&nbsp;</P>

</BODY>
</HTML>


Fritz the Blank
0
 
killer_kiwiCommented:
To stop the page unloading use this code

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

Note: this function returns a string that is hown in the dialogue. Also you can not alter the other text on the dialogue box
0
 
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: DeAn {http:#8122145} & fritz_the_blank {http:#8122155}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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