File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes conditional window unload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "conditional window unload" Watch "conditional window unload" New topic
Author

conditional window unload

Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
Hi Folks,
here is tricky stuff that i need. In my application, let's say, i've got a page with textarea. Now, the page has got a "refresh" button & "update" button. If i change the textarea message on screen, and click on "refresh", it's gonna display you a confirm dialog box, saying "data has changed. You want to proceed with refresh?".

So far so good. But, if i am changing the textarea, and clicking on Browser "close" button, the same confirmation should display. If i choose cancel on the cofirmation dialog, the page should remain on screen.

I am able to display the confirmation, at <window.onunload>, but it's always closing the window.How to stop that?
[ March 05, 2007: Message edited by: Bear Bibeault ]

Refreshing life every moment...
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You want to use onbeforeunload

Eric
Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
Hi Eric,
thanks for the response...I have used onbeforeunload(), but it's not even calling that one. here is my code,
window.onbeforeunload = function(){
alert('before unloading window');
var flag = confirm('Do you want to close the window?');
if(!flag){
alert('User doesn't wanna close the window.');
return false;
}
}
For you information, i already have one window.onunload() in the page, and everytime, i am clicking on the browser close, it's going there.
Have you got any idea about this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60817
    
  65

onbeforeonload will issue the confirm automatically. All you have to do is return a message.

For example:



If you return nothing, the onbeforeunload event does nothing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
Hi "Sheriff",
yeah it worked as you said...but, i am getting a rather long message in Confirm dialog box; it says:
"Are you sure you want to navigate away from the page?
<My Message>
Press Ok to continue, or Cancel to stay on the current page."

This 3 lines of message is kinda annoying to me. How can I over-ride it?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can't

Eric
 
 
subject: conditional window unload
 
Similar Threads
Retaining browser scrollbar position after page reloading
Submit Button in JSP Page Scrolls Frame!
printing from an applet toolbar the image displayed in the applet via a thread
html:multibox problem
Threaded Booking Method