Quote:
Originally Posted by gwalborn
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
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:
-
function PromptUserLogout(){
-
if(confirm('We have detected that you are leaving the page. Would you like to log out?'){
-
//make Ajax Request to server to log out user:
-
LogOut();
-
}
-
}
You would configure the window to call this method during the onbeforeunload event:
- window.onbeforeunload = PromptUserLogout;
Here's the Ajax code that will call the Logout ASP page (this page that should "release" your "locks"):
-
function LogOut()
-
{ /* Instantiating the HttpRequest object that will be used to make the Ajax calls.
-
See http://www.w3.org/TR/XMLHttpRequest/ for more information on this object.*/
-
var xmlHttp;
-
xmlHttp=GetXmlHttp();
-
-
/* Logout.asp is an ASP page has been created for the sole purpose of logging out the user and releasing the "locks".*/
-
xmlHttp.open('GET', "http://localhost/Logout.aspx", true);
-
//making the request
-
xmlHttp.send(null);
-
}
-
-
function GetXmlHttp()
-
{ /*This function is responsible for creating an HttpRequest object
-
based on the browser that the user is currently using. */
-
var xmlHttp = null;
-
try
-
{ //Mozilla, Opera, Safari etc.
-
xmlHttp=XMLHttpRequest();
-
}catch(e)
-
{ //Internet Explorer uses ActiveX objects to make Ajax calls.
-
//the following are valid versions, here we will loop through
-
//the versions and attempt to create the ActiveX that matches the browser.
-
var versionIds = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
-
"Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0",
-
"Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0",
-
"Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
-
for(var i=0; i<versionIds.length && xmlHttp == null; i++)
-
{
-
xmlHttp = CreateXmlHttp(versionIds[i]);
-
}
-
}
-
return xmlHttp;
-
}
-
-
function CreateXmlHttp(id)
-
{ /*Creates an ActiveX object used by Internet Explorer that will make Ajax calls*/
-
var xmlHttp = null;
-
try
-
{
-
xmlHttp = new ActiveXObject(id);
-
}catch(e) {}
-
return xmlHttp;
-
}
-Frinny