?
Solved

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

Posted on 2003-03-12
7
Medium Priority
?
2,387 Views
Last Modified: 2009-01-17
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
Comment
Question by:rdowdall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8122082
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
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8122088
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
 
LVL 3

Accepted Solution

by:
DeAn earned 500 total points
ID: 8122145
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 46

Assisted Solution

by:fritz_the_blank
fritz_the_blank earned 500 total points
ID: 8122155
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
 

Expert Comment

by:killer_kiwi
ID: 8122311
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10130505
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question