Link to home
Start Free TrialLog in
Avatar of SaraDob
SaraDob

asked on

Javascript: Window onbefore unload confrim message

Hi all,
I'm using javascript function window.onbeforeunload to display a message to the user before he exits the page.
This javascript is calling a server method( ASP.NET and Vb.net) , if the user decides to exit the page. Else do nothing.
I'm having trouble with javascript(newbie here).The trouble is:
Originally while exiting the page, i used to do anything.This is how i used to alert the user
  if (showUnloadMessage)
{
return('You have attempted to leave this page.If you have made any changes to the fields without clicking Submit button,This Data will be saved as This Month Report.Are you sure you want to exit the page?');
}

But now i have to call a server side script based on user answer.If he decides to Quit, i have call server side function.I have written the code as below, but It gives the the message" you have attempted", and then also gives confrim message.I dont want that confirm message. I want only The "you have attempted to leave" message.If i remove the confirm () , then how do i alert the user. Where do i put these retun statemets.
Please help me correct the following javascript. The rerun statements are soo confusing to me..!!!
Thanks again

var showUnloadMessage = true;
function window_OnBeforeUnload()
      {
         if (showUnloadMessage)
            if(confirm("You have attempted to leave this page.If you have made any changes to the fields without clicking Submit button,This Data will be saved as This Month Report.Are you sure you want to exit the page?"))
            {
             PageMethods.Message(); /*this is for Server method calls*/.
             return true;
            }
            else
            {
            return false;
            }
           
       
      }
      
      
window.onbeforeunload = window_OnBeforeUnload; /* This is how i call Before quiting*/

Open in new window

Avatar of HonorGod
HonorGod
Flag of United States of America image

The "confirm" message that you talk about should be the one with this text:

"You have attempted to leave this page.If you have made any changes to the fields without clicking Submit button,This Data will be saved as This Month Report.Are you sure you want to exit the page?"

So, I'm a bit confused.  Can you explain what you mean?
Avatar of SaraDob
SaraDob

ASKER

HonorGod:
Thanks for your reply.
Here is my explanation.With the above code this is what is happenining
1) I try to close the browser window
2) A message  appears saying" You have attempted to leave this page......".Thats the message i have written there.I click OK
3) Again another message comes up  saying " are you sure you want to navigate from this page?".I dont know why and where from ,that message is coming from.
i think that beacuse of the confirm() statement.
 If i replace the code with only  with return("message").I get it right.
But I want  write to the Pagemethod to call a sever side function, after the user says Ok to the return("message").
 
Avatar of SaraDob

ASKER

Or simple put,
Can you please write the code to give a message to the user  on  window.onbeforeUnload function ,
.And when he says" Ok" to  close the window , call a a function
 else do nothing.
 
ah!  It sounds like there are multiple (nested) instances of onbeforeunload!
Can you check the javascript files that you include, and see which, if any, refer to, and assign.

It may be as simple as having your code referenced last... unless, of course, their code is written in such a way as to preclude this...
ASKER CERTIFIED SOLUTION
Avatar of SaraDob
SaraDob

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial