By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Complete save operation when popup is closed

P: 6
hello!
how can the save task be completed when the popup is closed in javascript.
for eg i have this save function. so when i click on the OK of confirmation dialog box , the popup should also close and the save task should be completed.i am working with asp.
even if i use onunload or unbeforeunload it doesn;t work
any suggestions???
Jul 14 '08 #1
Share this Question
Share on Google+
6 Replies


hsriat
Expert 100+
P: 1,654
Can you explain with the help of some code what exactly are you trying to achieve?
Jul 14 '08 #2

acoder
Expert Mod 15k+
P: 16,027
I've changed the thread title. Avoid words such as help. Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions. Thanks!
Jul 15 '08 #3

P: 2
I think I have a similar problem. I've come to the conclusion that "onbeforeunload" is implemented in a very stupid fashion. Here's my problem-

I want to warn the user about leaving the page that they are on. I can use:

window.onbeforeunload = function() { return "WARNING" }

to do this. So the user gets a requester. So far so good. If the user clicks "CANCEL", I'm OK, because they go back to the page. The problem is that if the user clicks "OK", I get an immediate unload. I need to send a message back to my app saying that the user is closing the page! There should be a way to execute code AFTER the onbeforeunload event but BEFORE the unload event. The least they could have done is provided us some way to react to the "navigate away" event once it happens!

Any ideas?

gwalborn
Oct 3 '09 #4

acoder
Expert Mod 15k+
P: 16,027
That's what the onbeforeunload event is for. In fact, not all browsers support the event. Why would you need to let your application know immediately? You could use a timeout to cancel a session if that's the problem. Why can't you do it the other way round, i.e. let your app know the user is leaving the page and if the user is still on the page later, cancel/reset/ignore it?
Oct 6 '09 #5

P: 2
I can't use a timeout because there may be times when the user does NOT interact for long periods and yet need to stay active. On the other hand, I need to release locks as soon as possible when the user DOES disconnect. As to doing it the other way around, if I release the locks and THEN find out that the user is still there, I cannot re-acquire the locks.

BTW, I found an answer to my problem. The problem with unload is that my page is gone and so I couldn't SUBMIT the page w/ a variable set to indicate that the page was unloaded improperly. I solved that by crafting a NEW url (GET) that signals my app that the page was unloaded illegally and loading that in a NEW window with window.open().

I still say that "onbeforeunload" is implemented strangely. It is highly counter-intuitive.

gwalborn
Oct 6 '09 #6

Frinavale
Expert Mod 5K+
P: 9,731
@gwalborn
I don't understand what the problem was...

You should have written a method that sends an Ajax request to the server when the user is leaving the page and if the click "ok".

It's not that hard.

You would have had to create a method that asks the user if they want to log out because they are leaving the page:

Expand|Select|Wrap|Line Numbers
  1. function PromptUserLogout(){
  2.   if(confirm('We have detected that you are leaving the page. Would you like to log out?'){
  3.     //make Ajax Request to server to log out user:
  4.     LogOut();
  5.   }
  6. }
You would configure the window to call this method during the onbeforeunload event:
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload = PromptUserLogout;
Here's the Ajax code that will call the Logout ASP page (this page that should "release" your "locks"):
Expand|Select|Wrap|Line Numbers
  1. function LogOut() 
  2. {  /*  Instantiating the HttpRequest object that will be used to make the Ajax calls.  
  3.        See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
  4.   var xmlHttp;   
  5.   xmlHttp=GetXmlHttp();
  6.  
  7.   /*  Logout.asp is an ASP page has been created for the sole purpose of logging out the user and releasing the "locks".*/
  8.   xmlHttp.open('GET', "http://localhost/Logout.aspx", true); 
  9.   //making the request
  10.   xmlHttp.send(null); 
  11. }
  12.  
  13. function GetXmlHttp()
  14. {   /*This function is responsible for creating an HttpRequest object 
  15.       based on the browser that the user is currently using. */
  16.   var xmlHttp = null;            
  17.   try
  18.   {   //Mozilla, Opera, Safari etc.
  19.     xmlHttp=XMLHttpRequest();
  20.   }catch(e)
  21.   {   //Internet Explorer uses ActiveX objects to make Ajax calls.
  22.       //the following are valid versions, here we will loop through 
  23.       //the versions and attempt to create the ActiveX that matches the browser.
  24.     var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
  25.         "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
  26.         "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
  27.         "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
  28.     for(var i=0; i<versionIds.length && xmlHttp == null; i++) 
  29.     {
  30.       xmlHttp = CreateXmlHttp(versionIds[i]);
  31.     }
  32.   }
  33.   return xmlHttp;
  34. }
  35.  
  36. function CreateXmlHttp(id) 
  37. {   /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
  38.     var xmlHttp = null;
  39.     try 
  40.     {
  41.       xmlHttp = new ActiveXObject(id);
  42.     }catch(e) {}
  43.     return xmlHttp;
  44. }
-Frinny
Oct 6 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.