bcny

We have an application that logs into a system, we need to trap when a user may close the browser with the (X). In IE6 we can do this succesfully with the following code

In body tag

onUnload = "handleBrowserClose();"

function handleBrowserClose() {
if (screenTop > 9999) {
//handle logout event
}
}

Unfortunetly since IE7 the screenTop now actually contains the actually value of the top position. Does anyone have any suggestions on how to fix this in IE7 or a different way to trap that a user clicks the (X) button to close IE7



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

eric1076

Wondering if this issue got resolved or you were able to come up with an alternate solution. I am running into the same problem. Any help would be appreciated.



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

bcny

No I have not found a valid resolution to this yet.



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

mechalier

Hi, I had the same problem and just spent a couple of hours... It seems that "onbeforeunload" is working properly .

The pending problem is that JS method is fired when refreshed or URL changed. I handled this inside my PHP code with the HTTP_REFERER.

'hope that help...




Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

bcny

Can you post an example



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

Renee Punzi

here is an alternate approach to running a script when a user leaves your site:

- At the top of every script, set the session timeout to 20mins or use an application variable to set it to the IIS default.
- Then, use the onunload or onbeforeunload events to call a script which sets the session timeout to one minute or so.
- You then need to catch the session expiry event and call your log out code when it occurs. In ASP you can use the Session_OnEnd event in the global.asa.

Thus, if the user is leaving the site (by closing the window, or navigating to another site), then his session expires one minute later. If he is only refreshing the page, then as soon as the page is called, the session timeout is reset to its normal value, and he stays logged in!





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

mechalier

Here is on example with the onbeforeunload :

1. main script (script.php) in which the "leaving" is watched (NB : this script had been opened through a existing window (with a "_blank" target in the form) :

....

<script type='text/javascript'>
window.onbeforeunload = function() {
//in case of a close action with the white cross in the red square in the title bar

if (window.event.clientY < 0 && (window.event.clientX > (document.documentElement.clientWidth - 5) || window.event.clientX < 15)) {

//action to fire when leaving...
window.opener.location.href='target.php5 pctid_toclose=< php echo $_SESSION['pctid']; >';
// alert('user is leaving...');
}
}
</script>
....

2. targeted script (target.php) in which an action is done if needed:

....

if($_GET['pctid_toclose'] === $_SESSION['pctid']) {
//actions to do (updating DB for example)
.....
unset($_SESSION['pctid']);
echo "<script type='text/javascript'>document.location.href='index.php5';</script>\n";
exit();
}
.....

But Renee's method is probably more reliable since my method is not working if the user click the top left icon and/or press Alt+F4... :-(

Happy new year to all of you

--Matt





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

bcny

All of the above sound good, but unfortunetly I am not using ASP or PHP. We are using JSP, do you have any further thoughts on this with JSP



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

mechalier

JSP (rather than PHP) should not be any problem since the "added-value" method is in the Javascript code and not in the PHP. What I am doing first is to send a session identification (to close) and then I am just closing the session (and removing all the session parameters...). You should be able to do so with your JSP or Web Server.



Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

U31415

can you provide an example of setting the timeout in javascript




Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

mechalier

I am not the author of the timeout method, but to answer your "timeout javascript" question, here is an example (form here : http://www.htmlite.com/JS018.php) :

<script type="text/javascript">
setTimeout("alert('Thirty seconds has passed.');",30000);
</script>

Good luck !





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

typsy1981@yahoo.com



Hey Matt, thanks. I was searching for something similar. It works on IE7 but the condition is not to open a new tab. If you'll print the 3 values of below

<html>
<head>
<script type="text/javascript">

window.onbeforeunload=function(){

alert("window.event.clientX="+window.event.clientX);
alert("window.event.clientY="+window.event.clientY);
alert("document.documentElement.clientWidth="+document.documentElement.clientWidth);

if (window.event.clientY < 0 && (window.event.clientX > (document.documentElement.clientWidth - 5) || window.event.clientX < 15) ) {
alert("closing window");
}

}
</script>
</head>
<body>
test
</body>
</html>

you'll see there are some randon values for them if you do the following test:

Open a browser, in the first tab load the code, refresh, you'll see it won't enter in that condition which is good bu tthe things are changed when opening a new tab and refresh the previous tab pretty quicly. The values for X and Y are randomized which is not that good.

I still didn't find how to manage to cath browser close event with javascript. Any other deas Thanks.





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

romafe

Hi all.

I have similar problem mentioned above.

The script shown in the last post works, but it is not the real solution to capture the window when is closing.

Try this. Put the mouse pointer on the window cross and refresh the page by pressing F5. The window will close, since

window.event.clientX is the mouse position and it is the condition to close the window.

I did not resolve it yet. Detect the screenTop value works in IE6, but it doesn't in IE7. I need a similar solution that work in both versions.

Please, if somebody have a solution, let me know. I've already spent a lot of time in this.

Thanks a lot.

Marcelo





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

sachin

I tried to go closer to problem in below mentioned code but still it is not a perfect solution

<script type="text/javascript">
var alterffourflag=0;
var lastkey=0;
var refreshflag=0;
document.onkeydown = function ( event )
{
event = event || window.event;
return window_onkeydown();
}
</script>

<script for=window event=onunload>
ie7=navigator.userAgent.toLowerCase().indexOf('msie 7')!=-1;
if(ie7==1)
{
if(window.document.referrer.toString()=="")//to detect popup window in the IE7
{
return;
}

var offset=0.0;
var width=0.0;
if( document.documentElement && ( document.documentElement.clientWidth ))
{
//IE 6+ in 'standards compliant mode'
width = document.documentElement.clientWidth;

}
else if( document.body && ( document.body.offsetWidth))
{
width=document.body.offsetWidth;

}

offset=18500/screen.width;
var diff =width-offset;
if (refreshflag!=1 && width!=0 && window.event.clientY < 0 && (window.event.clientX > (width - offset))||alterffourflag==1)
{


if( window.opener == null)
{
if (window.XMLHttpRequest)
{
req=new XMLHttpRequest();
req.open("GET", "../ping.aspx" + " t=" + new Date(), false);//page which removes session information
req.send(null);
}
else
{
if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET", "../ping.aspx" + " t=" + new Date(), false);
req.send(null);
}
}
}
}
refreshflag=0;
}
else// IE6
{

if (self.screenTop > 10000 && event.clientY < 0 && event.clientX < 0)
{

if( window.opener == null)
{

if (window.XMLHttpRequest)
{
req=new XMLHttpRequest();
req.open("GET", "../ping.aspx" + " t=" + new Date(), false);
req.send(null);
}
else
{
if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET", "../ping.aspx" + " t=" + new Date(), false);
req.send(null);
}
}
}
}
}

</script>

<script language=javascript type="text/javascript" >
function window_onkeydown()
{
var keynum
var numcheck
e=window.event;
keynum = e.keyCode;
if(lastkey==18 && keynum==115)//||(lastkey==17 && keynum==87))
{
alterffourflag=1;
}
if(keynum==116)
{
refreshflag=1;
}
lastkey=keynum;
}

</script>





Re: Internet Explorer Web Development JavaScript Detect browser close IE 7

jd31068

I am using the onunload event.

What I did was (because that event runs on every click) this ..

For each button and link on my particular page I have Javascript write to a label on screen telling the user that the page is requesting data from the server. My function that checks whether I should do anything because the browser is closing, checks to see if my "requesting data from server" message is displayed in the label, if it is then I know its a button/link click and I dont want to run my "the browser is closing code" ... if that text isn't there. The browser is closing.

This works for me, i hope it helps someone else.