Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Calling Java applet function upon browser close

Thread view: 
M B HONG 20 - 15 Feb 2006 23:54 GMT
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.
Finomosec - 16 Feb 2006 20:45 GMT
M B HONG 20 schrieb:
> 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
[quoted text clipped - 3 lines]
> 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;


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.