Solved

Detect before/when the browser leaving to another webpage

Posted on 2003-12-02
7
687 Views
Last Modified: 2013-12-16
When a JApplet is loaded and the browser browse to another webpage the applet (the main JFrame) is closed without a warning.  Can a I detect that it in advance to prevent the browser from leaving the webpage or at least warn the user or so?
0
Comment
Question by:s_lavie
  • 5
  • 2
7 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9858274
add onLoad and onUnload javascript to the webpage...

That will fire javascript when the page is loaded, and when they leave the page...

You can't stop them leving though, so what you want to do isn't possible :-(
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9858283
ie:

<body onLoad="alert( 'Hi!' )" onUnload="alert( 'Bye!' )">
0
 
LVL 2

Author Comment

by:s_lavie
ID: 9858334
Can I issue a yes/no dialog that the user can regret before the browser leaves the webpage?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 35

Expert Comment

by:TimYates
ID: 9858365
There is

onBeforeUnload

but it is only available in Internet Explorer

http://www.webreference.com/dhtml/diner/beforeunload/index.html

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

<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">
<!--
function unloadMess(){
    mess = Leaving so soon?"
    return mess;
}

function setBunload(on){
    window.onbeforeunload = (on) ? unloadMess : null;
}

setBunload(true);
//-->
</SCRIPT>
0
 
LVL 2

Author Comment

by:s_lavie
ID: 9858772
I'm quite weak with JavaScript.  I'd appreciate if you add a sample code that includes the yes/no dialog and the canceling of moving to another webpage,
10x
0
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 9858897
<html>
    <head>
        <title>Tim's cool test!</title>

        <SCRIPT>
        <!--
            function unloadMess()
            {
                mess = 'Leaving so soon?' ;
                return mess;
            }

            function setBunload(on)
            {
                window.onbeforeunload = (on) ? unloadMess : null;
            }

            function pageLoaded()
            {
                alert( 'Setting the beforeUnload handler (IE ONLY)' ) ;
                setBunload(true);
            }

            function pageClosed()
            {
                alert( 'Closing the page' ) ;
            }
        //-->
        </SCRIPT>

    </head>
    <body bgcolor="#FFDDFF" onload="javascript:pageLoaded()" onUnload="javascript:pageClosed()">
        <a href="http://www.google.com">Go to google</a><br/>
        <a href="http://www.experts-exchange.com">Go to experts Exchange</a><br/><br/>
        Clicking either of these (in IE) will ask if you're sure!!
    </body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9858906
Mmmmm.....pink ;-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 52
JAVA API design with micro service cloud in mind 1 74
web project error add remove 1 49
Java class and jar 3 41
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question