Calling Java applet function upon browser close

M

M B HONG 20

Hi all -

I am developing a web application and need to fire an on-close event
upon the user clicking the "X" button on their browser. However, i
MUST use Netscape 7.0, which does not support any native "onunload"
type of functionality. So, i had the idea of using a java applet to
access the page's DOM and fire a javascript function upon close. This
is the code I am using in my applet thus far:

import netscape.javascript.*;
import java.applet.*;
import java.awt.*;

public class CloseApplet extends Applet
{
JSObject win;
JSObject doc;
JSObject loc;

public void init()
{
win = JSObject.getWindow(this);
doc = (JSObject) win.getMember("document");
loc = (JSObject) doc.getMember("location");

setBackground(Color.black);

win.call("TestAlertOpen", null);

}

public void stop()
{
win.call("TestAlertCloseStop", null);
}
public void destroy()
{
win.call("TestAlertCloseDestroy", null);
}
}

and this is the page...

<HTML>
<HEAD>
<title>Test Java</title>
</HEAD>
<BODY>
<APPLET CODE="CloseApplet.class" CODEBASE="." WIDTH="1020" HEIGHT="150"
style="Z-INDEX: 101; LEFT: 30px; WIDTH: 1020px; POSITION: absolute;
TOP: 200px; HEIGHT: 150px" MAYSCRIPT>
</APPLET>
<SCRIPT language="javascript">
function TestAlertOpen()
{
alert ("open.");
}
function TestAlertCloseStop()
{
alert ("close, stop.");
}
function TestAlertCloseDestroy()
{
alert ("close, destroy.");
}
</SCRIPT>
</BODY>
</HTML>

When I close the page, I get an error. I realize that the error is
resulting from the fact that the applet is attempting to call a
function that is not there, due to the fact that the page has already
been closed. What I am asking is whether there is a better way (such
as an "OnBeforeUnload" event or something) that I can use to accomplish
calling a javascript function upon the browser close. Any help would
be greatly appreciated.
 
F

Finomosec

M said:
When I close the page, I get an error. I realize that the error is
resulting from the fact that the applet is attempting to call a
function that is not there, due to the fact that the page has already
been closed. What I am asking is whether there is a better way (such
as an "OnBeforeUnload" event or something) that I can use to accomplish
calling a javascript function upon the browser close. Any help would
be greatly appreciated.

I have a slight idea how it could work:
On page-load open a new new (small) window and send it to background:

var secondWindow = window.open("URL", "WindowName",
"width=300,height=100,left=100,top=100");
window.focus();

Maybe you want to "hide" the second window.
You may try to move it out of the desktop area or resize it to 0x0.

Or you simply display a short info-text so none is irritated.

see also (german):
http://de.selfhtml.org/javascript/objekte/window.htm#open

In the second window you setup an Interval, that checks if the first
window is still there:
// Second Window-Script:
window.setInterval("myCheckFunction()", 250) ;

function myCheckFunction() {
if (window.opener.closed) {
doYourExecute();
// close this (second) window ...
window.close(); // may not be possible directly ... but there was a
opener-hack somehow ...
}
}

You may also add a similar observation to the first window, that
repeatedly checks if the second window is still open, and if not -
reopens it ...


I just found another alternative:
Instead of a second window it may work with frames ... but not that certain.

Greetings Finomosec;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top