473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to cancel a window unload?

gregerly
192 Expert 100+
I'm working on a corporate intranet site and am trying to create a page that will warn a user on window unload if they have not saved thier work. Basically I just select all inputs, and on change, set var changes = true; On window unload if changes==true, they have made changes that are not yet saved, and I want a confirm box to pop up, asking if they really want to leave or stay on the page? At this point is it to late to cancel the window unload? My code is below:

Expand|Select|Wrap|Line Numbers
  1. window.onunload=checkChanges;
  2. var changes=false;
  3.  
  4. function checkChanges(){
  5.     if(changes==true){//changes have been made
  6.         if(!confirm("Do you want to leave without saving your changes?\n\nOK to Go, Cancel to stay on this page.")){
  7.                 window.unload=false;
  8.         }
  9.     }
  10. }
  11.  
  12. //while looping thru all inputs
  13.         inputs[x].onchange=function(){
  14.             changes=true;
  15.         };
I'm getting the proper confirm boxes, but I dont' know how to cancel the unload, if it's even possible.

Thanks,

Greg
Sep 27 '07 #1
2 30511
mrhoo
428 256MB
try onbeforeunload.
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload= function(){
  2.     return 'You are about to leave this page, OK?';
  3. };
Sep 27 '07 #2
gregerly
192 Expert 100+
try onbeforeunload.
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload= function(){
  2.     return 'You are about to leave this page, OK?';
  3. };
Thanks alot mrhoo. That worked great. I had some other questions, but I just got it figued out. Apparently the onbeforeunload prompts with it's own confirm that will keep a user on the page. Found that in this thread.

Thanks again!
Sep 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: KS | last post by:
Is it possible to write a javascript that makes a popup window when someone click on buttons/href on my page and close itself when the new page is about to get loaded? I want to prevent the user...
2
by: gokul | last post by:
Hi all, Iam using mozilla firebird. I have three popups displayed at one point of time...if I close one the other two goes hidden....could you please suggest me the reason for this behaviour and...
6
by: Marcia Gulesian | last post by:
What code can I put in the 'onbeforeunload' event to cancel the 'unload' action (when user closes browser) and then present the user with an alert saying that he/she must use the logout button.
2
by: RJN | last post by:
Hi I have a datagrid which has linkbutton as a template column. The Linkbutton is used to open new windows. I can open multiple windows at a time. On close of any of the child windows,(i.e, on...
1
by: RJN | last post by:
Hi I have a pop-up window which has a task menu on its left which are nothing but links to different screens. This window also has a close button. On click of close button I do some server side...
8
by: Luke Matuszewski | last post by:
I have read all posts about how to detect that url have changed to new page and trigger the event handler then eg. function aidLogout(evt) { if(evt) { /* maybe via analyse of evt object i can...
6
by: Luke Matuszewski | last post by:
As in topic... has anyone used it ? I tried to use it but i guess i failed... here is a code: <html> <head> <title></title> <script type="text/javascript"> function closedWin() {...
1
by: BennyVK | last post by:
Hi, I'm not sure if this question should go to the "JavaScript" section; but I'm open to non-JavaScript (like VBScript) options too. So, I'm posting this here. I have the following problem:...
9
by: Nagesh | last post by:
Hi All, I have a requirement to handle the window(browser) close event. When a user tries to close the browser window , we have to prompt him whether he wants to close the window or not. If...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.