Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conditional window unload

 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use onbeforeunload

Eric
 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic