PDA

View Full Version : Actions before closing


Lesley
11-30-2002, 03:26 PM
I need to run a cgi program when the user closes a window.

Can anyone tell me how to do this in Java?

The idea is (invisibly) to delete a folder of files when the user is finished. I can handle the cgi, but not the Java.

Lesley

mordred
11-30-2002, 04:03 PM
If you mean the main browser window, then there is no secure way of telling by JavaScript (or did you really mean Java?) when the user closes the window. A possible event like "onclose" does not exist.

If you spawn a new child window via javascript, you could try checking with a small function in an interval from the main window if the child window has been closed (there is a .closed property for such window variables).

brothercake
11-30-2002, 04:16 PM
what mordred said:

var newWin = open("newpage.html","newWin");
var checkTimer = setInterval("checkForClosed()",100);

function checkForClosed() {
if(!newWin || newWin.closed) {
alert("window is closed");
clearInterval(checkTimer);
}
}

Lesley
11-30-2002, 04:27 PM
Thank you both for taking the time to answer.

I am still a bit lost, though - being very new to the occasional use of Java/Javascript heck I don't even know the difference !

The window I am concerned with is a named window, which can therefore be addressed - I assume it qualifies as what you would term a 'child' window in itself.

Basically what I want is ..

onclose .....run this cgi program....shut the window

I am not too sure what brothercake's script will do exactly

Lesley

mordred
11-30-2002, 06:57 PM
Well as I said, there is no "onclose" event in JavaScript. My suggestion and brothercake's code provide a workaround, but that needs a window opened by JavaScript. If it has a name that's fine, but what is essential is that a reference to the window object of the child window is assigned to a variable in the main window. That's what's done in this line:

var newWin = open("newpage.html","newWin");

newWin now points (references) your child window which has the name "newWin". Just try out brothercake's code, it will show you when the child window has been closed.

P.S: The difference between Java and JavaScript is quite... erh... significant ;). In this forum we're mainly talking about JavaScript, the scripting language most commonly used in browsers for form validation, strange error messages and annoying popups. ;)

mhere
12-01-2002, 10:29 AM
the onbeforeunload event fires when the user closes the window

<body onbeforeunload="alert('Good Bye')">