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

LVL 1
SaraDobAsked:
Who is Participating?
 
SaraDobAuthor Commented:
Thats how i put it..and it worked..
Thanks for your attention and replies..

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 NOT BE saved as This Month Report.Are you sure you want to exit the page?"))
         {
        PageMethods.Message();
         }
      }
0
 
HonorGodSoftware EngineerCommented:
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?
0
 
SaraDobAuthor Commented:
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").
 
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
SaraDobAuthor Commented:
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.
 
0
 
HonorGodSoftware EngineerCommented:
ah!  It sounds like there are multiple (nested) instances of onbeforeunload!
0
 
HonorGodSoftware EngineerCommented:
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...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.