Solved

Detect before/when the browser leaving to another webpage

Posted on 2003-12-02
7
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 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