Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Response from conditional onbeforeunload event

Status
Not open for further replies.

momukhtar

Programmer
Mar 10, 2008
8
i have a testpage for a web application in PHP and Javascript.

It has previous, NExt and complete eXam button to browse to different questions and submit exam. When the user would click on Window Close (X), i want a popup (OK, Cancel). If the user click OK then window is closed and php session variable must be destroyed and on CANCEL nothing happens.
To prevent this popup on next, previous and complete exam buttons , i made one hidden field and "onbeforeunload" event is only fired when the window close button is clicked.

I want to know that how can i get the response that the user has clicked on "OK" so that I can destroy the php session variables associated with that test.?

My code is

window.onbeforeunload=confirmExit;
function confirmExit() {
// this conditon to test whether next,previous, completexam button
// or window cloase button has been clicked
if (document.onlinetestform.close.value == "-1") {
return 'If you close your data will be lost and you will forfeit your exam.'
}
}

i just want to know whether user clicked on OK or Cancel in this case
 
Try looking at window.opener to manipluate variables in the parent window.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
I think you want a combination of both "onbeforeunload" (to issue the warning) and "onunload" which is actually when it in fact unloads.

Basically, you'd have similar logic in the "onunload" to what you already have in "onbeforeunload"... you'd look to see if the onunload happened because of clicking one of the buttons or by default it must have been from the X close button. If it's from the X button, you know the user was already presented the "onbeforeunload" dialog asking "Ok" or "Cancel", and if the page is unloading now, then they must have clicked "Ok" to make that happen.

Then, inside your "onunload" function, what you can do to make sure you tell your server to kill the session is make some quick signal back to the server to tell it to kill the session. The simplest way to do this is to have like a "new Image()" object that you instantiate, who's URL is a PHP script specifically designed to kill the session in question. By setting the 'src' attribute of a javascript Image object, that request will immediately be fired off to the server.

The response doesn't matter since the page is unloading anyway, and you're now sure that the PHP got notified that it needs to kill the session right away.

------------

Incidentally, there are of course other ways to close a window, such as Alt+F4 (windows), or refresh a page (F5, ctrl+r, etc), but the above methodology should work the same in all those cases. You should probably decide if you want to cause the session to die if they force a refresh of the window -- probably yes... but if no, you'll have to do some more exotic keyboard trapping and set some other hidden variable so you can know if the unload is happening as a result of a refresh or a close.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top