Link to home
Start Free TrialLog in
Avatar of s_lavie
s_lavie

asked on

Detect before/when the browser leaving to another webpage

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?
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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 :-(
ie:

<body onLoad="alert( 'Hi!' )" onUnload="alert( 'Bye!' )">
Avatar of s_lavie
s_lavie

ASKER

Can I issue a yes/no dialog that the user can regret before the browser leaves the webpage?
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>
Avatar of s_lavie

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mmmmm.....pink ;-)