...

View Full Version : Wanted To Execute Java Script Code when browser close button is clicked



subbaraojs
07-28-2006, 07:25 AM
Hi,

I want to execute some java script code when I close the browser. The following code is executing in I.E. But I want to execute in Firefox and Netscape.


<html>
<head>
<script language="JavaScript">

function fun()
{

if((window.event.clientX<0) || (window.event.clientY<0))
{

alert("Inside if");

}
}

</script>

</head>
<body onunload="fun()">

</body>
</html>


Can any One Please help me.

Thanks in advance

Subbarao

Pyth007
07-28-2006, 02:04 PM
Not sure, but you may be having trouble with the event procedure... Try:


<html>
<head>
<script language="JavaScript" type="text/javascript">
// It's better to use the type attribute than language

function fun(e)
{
// Gecko browsers, I believe, pass in event param's; IE uses window.event
if (!e) var e=window.event;
if((e.clientX<0) || (e.clientY<0))
{

alert("Inside if");

}
}

window.onload=fun;
</script>

</head>
<body>

</body>
</html>

Kor
07-28-2006, 02:17 PM
onbeforeunload is to be recognized both for IE and Moz. But Moz uses another type of event handling and other attribute (pageX instead of clientX). Even so, it woun't work, as the onbeforeunload is not implemented in Mozilla to bear the pageX or pageY property. It will return "undefined"

subbaraojs
07-31-2006, 07:31 AM
Hi Pyth007,

The solution you gave is not working.
Can you please suggest something else.

Thanks and Regards,
Subbarao

subbaraojs
07-31-2006, 07:34 AM
Hi KOR,

I used pageX in Mozila, but it is always returning 0(Zero). You mentioned that it will return undefined, but it returning always 0(Zero) only in any case.

Thanks and Regards,

Subbarao

Pyth007
07-31-2006, 02:09 PM
Based on what Kor stated about clientX / pageX properties, your may try:


function fun(e)
{
// Gecko browsers, I believe, pass in event param's; IE uses window.event
if (!e) var e=window.event;
if(e.clientX)
{
if((e.clientX<0) || (e.clientY<0))
{

alert("Inside if");

}
}
} else if (e.pageX) {
if((e.pageX<0) || (e.pageY<0))
{

alert("Inside if");

}
}
} else {
alert('Browser / event not supported');
}


Again, this is untested; I'm not sure if onunload carries the pageX info. If not, it still may not work.

subbaraojs
08-01-2006, 10:56 AM
Hi Pyth007,
The code which u gave is not working.
third alert statement ie 'Browser / event not supported' is executing in case of Mozila and Netscape.

Thanks and Regards
Subbarao

felgall
08-01-2006, 11:45 AM
Most browsers support clientX (position in browser window) and also pageX (position in web page). For more details on how these are handled incorrectly in some browsers see http://javascript.about.com/library/blmousepos.htm

subbaraojs
08-02-2006, 06:46 AM
Hi felgall,
Firefox and mozila is not supporting pageX. Can you please write a sample code regarding this?

Thanks in advance,
Subbarao

Kor
08-02-2006, 09:28 AM
Oh, but they support. Except that unbeforeunload event seems not to.

Example? Sure. Capture the mouse position onmousemove:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
var posx;var posy;
function getMouse(e){
posx=0;posy=0;
var ev=(!e)?window.event:e;//IE:Moz
if (ev.pageX){//Moz
posx=ev.pageX+window.pageXOffset;
posy=ev.pageY+window.pageYOffset;
}
else if(ev.clientX){//IE
posx=ev.clientX+document.body.scrollLeft;
posy=ev.clientY+document.body.scrollTop;
}
else{return false}//old browsers
document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
}
</script>
</head>
<body onmousemove="getMouse(event)">
<div id="mydiv">&nbsp;</div>
</body>
</html>

subbaraojs
08-02-2006, 10:38 AM
Hi KOR,
I want to execute javascript code when browser is closed. This code won't work.

subbarao

Kor
08-02-2006, 12:11 PM
I keep telling you that the only crossbrowser event is onbeforeunload. Unfortunately, it looks like this event is implemented incompletely in Mozilla, thus it can not bear information about pageX and pageY. At least I tried and I failed to do so. So that, as far as I tried, your problem can not be solved in Mozilla.

Maybe someone else has other ideeas. I have not.

Kor
08-02-2006, 12:12 PM
What do you want to do, in fact?

subbaraojs
08-02-2006, 01:13 PM
Hi KOR,

Please observe below code. If you run the below code in IE, when you close the browser alert statement will execute. I want the same functionality in Mozila and Netscape. My Idea is when close the browser I want to execute some java script code.

<html>
<head>
<script language="JavaScript">

function fun()
{

if((window.event.clientX<0) || (window.event.clientY<0))
{

alert("Inside if");

}
}

</script>

</head>
<body onunload="fun()">

</body>
</html>

Kor
08-02-2006, 04:20 PM
No, Sir, I would like to know your final aim as I don't really think you need an alert. What would be the use to alert those values?

Kravvitz
08-02-2006, 09:09 PM
subbaraojs seems to be trying to make one of the code snippets on this page (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dhtmltechcol/cols/dnwebteam/webteam12032001.asp) work cross-browser. Here's the section that I am referring to:

Closing Down or Leaving?

Q: Ikram wants to know if you can detect when the user closes the browser from script.

A: The short answer is no, but since we've got a little extra room, there is a way to make a very educated guess. If you use the following as your BODY onUnload event handler, you'll catch it in virtually all cases:


function body_onUnload()
{
if (window.event.clientX < 0)
{
alert("The browser is closing...");
}
else
{
alert("The user is refreshing or navigating away...");
}
}

Kor
08-02-2006, 09:26 PM
Q: Ikram wants to know if you can detect when the user closes the browser from script.

A: The short answer is no, but since we've got a little extra room, there is a way to make a very educated guess. If you use the following as your BODY onUnload event handler, you'll catch it in virtually all cases:

I am affraid that the short answer is yes. And crossbrowser - at least IE and Moz (I am not sure about the Opera, no Opera installed at this moment on this computer to test):


<script type="text/javascript">
window.onbeforeunload = function (evt) {
var message = 'Some of the form\'s data might not be submitted yet!';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
</script>


Except that the event fires the function nomatter if closing the browser or simply leaving the page. From the interpretor's point of view, the two events are the same: closing the session.

Kravvitz
08-02-2006, 09:35 PM
Kor, you seem to have missed the point. Allow me to explain.

onunload and onbeforeunload fire whenever the page unloads, which happens when someone closes the tab/window, navigates to a different page, or (I think) when the page is simple reloaded/refreshed.

The script that I quoted there can often detect whether the window/tab in IE/Win is being closed or not.

I doubt this is possible to do cross-browser. Someone could put in feature requests for the other browsers to emulate this behavior.

Kor
08-02-2006, 09:58 PM
Test my code. Or I missed something?

Kravvitz
08-02-2006, 10:10 PM
Yes, you are missing something. Read my last reply again. Hmm... maybe something was lost in translation. Your English is very good, but it's not your first language.

The idea is to tell whether someone is leaving a certain site or just navigating to another page on that site.

Your function only tells when the page unloads, it doesn't differentiate between the causes.

Kor
08-02-2006, 10:22 PM
That means I did understand the problem and I have pointed out that, in my oppinion:


Except that the event onbeforeunload fires the function nomatter if closing the browser or simply leaving the page. From the interpretor's point of view, the two events are the same: closing the session.


And I am sure that there is no crossbrowser client-side solution to make this distinction.

subbaraojs
08-03-2006, 10:34 AM
Hi KOR,

The code which you send will execute in following three cases.
a. when we navigate from one page to another page.
b.when we refresh the page and
c. when we close the browser.

But I want the code needs to be execute when browser is closed.

In I.E it is possible, I want in Netscape and Mozilla.

Subbarao

Kor
08-03-2006, 08:57 PM
As far as I know, this is not to be done crossbowser. Not now, not with these browsers.

felgall
08-03-2006, 09:24 PM
onbeforeunload isn't cross browser - only some browsers support it.

only onunload is supported by all browsers but by the time that is triggered it is too late to do anything.

Kor
08-03-2006, 09:41 PM
onbeforeunload isn't cross browser - only some browsers support it.

only onunload is supported by all browsers but by the time that is triggered it is too late to do anything.

onbeforeunload was implemented in Mozilla as well for a good time (I gues from about 1 and 1/2 year, 2 years ago). IE + Moz + Opera have now almost 96-99 % of the market... So that onbeforeunload is crossbrowser. Except that it's implementation regarding some events is incomplete.

Don't tell me about the old browsers. As long as Mozilla, or it's brilliant (till now) FireFox are free to download, those with elder browsers have no excuse. It's like trying to get a coloured TV program using an old black-white TV set.
------
Despite their official site, onunload is not supported my Mozilla Or at least whichever test I have done, confirmed me this). So that is a paradoxe. If no action alowed (as You said), as onunload is too fast, you say, now what kinda "support" that might be? Or ononload is supported (IE) or it is not. The middle way is not possible, I reckon... It sould be, but it is not.

Kravvitz
08-03-2006, 10:55 PM
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, and Firefox support onbeforeunload. Other browsers don't.

Kor
08-03-2006, 11:39 PM
Korekt. And other browsers that those ones do not matter.

Once again, I try to set the probelm on basic/logical.

1. One says that onunload, togther with the clientX clientY check, can make the difference between leaving a page by closing the browser and going to another page (by the way, this was the answer/problem I needed firts to know from you, subbaraojs... I needed to know what in fact you want to capture)

2. Unfortunately, onunload looks like not working in Mozilla (even felgall says it does somehow, but even he tell us later that it does not... - well, the general ideea looks like onunload woun't work for Mozilla)

3. onbeforeunload looked like the saviour even. Un fortunately, as fat as I have tested (feel all free to check iot out) Mozilla is not able to capture the mouse position onbeforeunload but IE does that as well.

Conclusion. There is no possible method to check whether the user moved his mouse to close the window or simply clicked to a link on the page.

Conclusion2. Really? If you would take a look to my example how to capture thr mouse position, you may find that there might be a crossbrowser solution... I wonder how you can not have seeing it.

Anyway... What if the user presses ALT-F4 ? :)

Kravvitz
08-03-2006, 11:45 PM
Korekt. And other browsers that those ones do not matter.
Safari and Opera don't matter?!?


Anyway... What if the user presses ALT-F4 ? :)
It seems to work the same as if the user closed the program some other way.

Kor
08-04-2006, 12:07 AM
Nope, I have tested. It woun't work. The problem is that not onbeforeunload is not quick enough, but the onmousemove is not. Simply test the folowing to see what I maen. Gentle move to the close button, the alert will appera. Move it fast( u will se even in the displayed X or Y position that the value will not follow quickly enough the mouvement) and the message wount appear.

So that, no solution so far. Any other ideeas, friends?

Kor
08-04-2006, 12:08 AM
here's the test


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
var posx;var posy;
function getMouse(e){
posx=0;posy=0;
var ev=(!e)?window.event:e;//IE:Moz
if (ev.pageX){//Moz
posx=ev.pageX+window.pageXOffset;
posy=ev.pageY+window.pageYOffset;
}
else if(ev.clientX){//IE
posx=ev.clientX+document.body.scrollLeft;
posy=ev.clientY+document.body.scrollTop;
}
else{return false}//old browsers
document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
}

window.onbeforeunload = function (evt) {
if(posx<=0||posy<=0){
var message = 'Do you really wanna close the browser?';
if (typeof evt == 'undefined'){
evt = window.event;
}
if(evt) {
evt.returnValue = message;
}
return message;
}
}
</script>
</head>
<body onmousemove="getMouse(event)">
<a href="bla.html">bla</a>
<br>
<br>
<div id="mydiv">&nbsp;</div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum