[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript: Window onbefore unload confrim message

Posted on 2009-04-28
6
Medium Priority
?
944 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:SaraDob
  • 3
  • 3
6 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24252764
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
 
LVL 1

Author Comment

by:SaraDob
ID: 24252843
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
 
LVL 1

Author Comment

by:SaraDob
ID: 24252898
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

Expert Comment

by:HonorGod
ID: 24253035
ah!  It sounds like there are multiple (nested) instances of onbeforeunload!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24253710
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
 
LVL 1

Accepted Solution

by:
SaraDob earned 0 total points
ID: 24325469
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question