简体   繁体   中英

How can I detect a close in the browser tab?

Hello I am looking for information on the close tab (not browser) event if there is one in java for a applet. I am wondering if there is an event for that or a way to check a way to check for that. I would like to just capture the event and make a little popup box , stating Your session will expire or something along those lines. Is that possible at all or to a point with java or Javascript?

UPDATE: okay with the information you guys pointed me into i was able to get information on a simple enough javascript. Now it is working fine in IE , Chrome and Firefox but for some reason Safari 5.1.7 isn't liking the code. Not sure why. Here is the code if it helps.

jQuery(function() {
var hiddenBtn = document.getElementById("javaform:browserCloseSubmit");

try{
    opera.setOverrideHistoryNavigationMode('compatible');
    history.navigationMode = 'compatible';
}catch(e){}

//Sends the information to the javaBean.java file. 
function ReturnMessage()
{
    return hiddenBtn.click();
}

//UnBind Function
function UnBindWindow()
{
    jQuery(window).unbind('beforeunload', ReturnMessage);
}



//Bind Exit Message Dialogue
jQuery(window).bind('beforeunload', ReturnMessage);

});

You have the onBeforeUnload event you can catch in JavaScript. See here .

Use window.onbeforeunload

window.onbeforeunload = function () {
    return "Are you sure you want to exit?";
};

Note that it will also end in Are you sure you want to leave this page (or are you sure you want to reload this page if you are reloading)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM