469,140 Members | 1,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

Is there an event when that triggers when the window is closing??



Is there an event when that triggers when the window is closing.... I am
talking about when the user clicks on the cross on the right top corner of
the window!!!
Jul 23 '05 #1
10 9341

"Tom Szabo" <to*@intersoft.net.au> escreveu na mensagem
news:41******@dnews.tpgi.com.au...


Is there an event when that triggers when the window is closing.... I am
talking about when the user clicks on the cross on the right top corner of
the window!!!
The Onunload body element an example is given above:
<body OnunLoad="javascript:window.alert('Thanks for visiting our site')">

Jul 23 '05 #2
"Oscar Monteiro" <of**@hotmail.com> wrote in message news:<41***********************@news.telepac.pt>.. .
"Tom Szabo" <to*@intersoft.net.au> escreveu na mensagem
news:41******@dnews.tpgi.com.au...


Is there an event when that triggers when the window is closing.... I am
talking about when the user clicks on the cross on the right top corner of
the window!!!
The Onunload body element an example is given above:
<body OnunLoad="javascript:window.alert('Thanks for visiting our site')">


No such thing as an 'Onunload body element' - onunload is an event
handler property (of window). The name of the event you're interested
in is unload, which invokes the [window.]onunload handler.
MSIE/FireFox 0.9+ also support window.onbeforeunload, which allows
more leeway in running script, including cancelling the close
entirely.

Most people who ask about this are interested in discriminating
between an actual attempt to close the window (or navigate to another
site) and a simple change of page within their own site. Since the two
event handlers are called by any of these - the unloading of the
current document is the trigger - all sorts of tortuous methods have
been configured to catch an actual close event, from popping up 'spy'
windows to observe the action, to flagging internal links to trap
their usage to...you name it. My experience with most of these has not
been encouraging. Here's a novel approach from glenngv of
codingforums.com:

<html>
<head>
<title>Detecting Closing of Window in IE</title>
<script language="javascript">
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}
</script>
</head>
<body onunload="doUnload()">
<h3>Detecting Closing of Window in IE</h3>
Do the 3 ways below and see the difference:<br>
1. Try to refresh the page.<br>
2. Try to type any URL in the address bar.<br>
3. Try to close this window by clicking X button of this window.
</body>
</html>

Catches the mouseclick position. IE only, but easily modifiable. What
exactly did you need to do?
Jul 23 '05 #3
RobB" <fe******@hotmail.com> wrote in message
news:ab**************************@posting.google.c om...
"Oscar Monteiro" <of**@hotmail.com> wrote in message news:<41***********************@news.telepac.pt>.. .
"Tom Szabo" <to*@intersoft.net.au> escreveu na mensagem
news:41******@dnews.tpgi.com.au...


Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
The Onunload body element an example is given above:
<body OnunLoad="javascript:window.alert('Thanks for visiting our

site')">
No such thing as an 'Onunload body element' - onunload is an event
handler property (of window). The name of the event you're interested
in is unload, which invokes the [window.]onunload handler.
MSIE/FireFox 0.9+ also support window.onbeforeunload, which allows
more leeway in running script, including cancelling the close
entirely.

Most people who ask about this are interested in discriminating
between an actual attempt to close the window (or navigate to another
site) and a simple change of page within their own site. Since the two
event handlers are called by any of these - the unloading of the
current document is the trigger - all sorts of tortuous methods have
been configured to catch an actual close event, from popping up 'spy'
windows to observe the action, to flagging internal links to trap
their usage to...you name it. My experience with most of these has not
been encouraging. Here's a novel approach from glenngv of
codingforums.com:

<html>
<head>
<title>Detecting Closing of Window in IE</title>
<script language="javascript">
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}
</script>
</head>
<body onunload="doUnload()">
<h3>Detecting Closing of Window in IE</h3>
Do the 3 ways below and see the difference:<br>
1. Try to refresh the page.<br>
2. Try to type any URL in the address bar.<br>
3. Try to close this window by clicking X button of this window.
</body>
</html>
Hi Rob,

What I want to do is:

firstly if the child window is closed by the user using the "x" on the top
left corner (that is prematurely considering the actual application) I need
to enable the data controls on the parent window (opener)!!

secondly, if the parent window is closed in the same way, I need to close
the child. -> this is needed because from the child I can disable all the
controls on the parent, but not the "X", so they can close the window...

Thanks for the stuff by the way, I will try soon,

Regards,

Tom


Catches the mouseclick position. IE only, but easily modifiable. What
exactly did you need to do?

Jul 23 '05 #4
Hi Rob,

I have tried, and it doesn't do anything!

No error with the script, just nothing.

Any clues?

TIA,

Tom
"RobB" <fe******@hotmail.com> wrote in message
news:ab**************************@posting.google.c om...
"Oscar Monteiro" <of**@hotmail.com> wrote in message news:<41***********************@news.telepac.pt>.. .
"Tom Szabo" <to*@intersoft.net.au> escreveu na mensagem
news:41******@dnews.tpgi.com.au...


Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
The Onunload body element an example is given above:
<body OnunLoad="javascript:window.alert('Thanks for visiting our

site')">
No such thing as an 'Onunload body element' - onunload is an event
handler property (of window). The name of the event you're interested
in is unload, which invokes the [window.]onunload handler.
MSIE/FireFox 0.9+ also support window.onbeforeunload, which allows
more leeway in running script, including cancelling the close
entirely.

Most people who ask about this are interested in discriminating
between an actual attempt to close the window (or navigate to another
site) and a simple change of page within their own site. Since the two
event handlers are called by any of these - the unloading of the
current document is the trigger - all sorts of tortuous methods have
been configured to catch an actual close event, from popping up 'spy'
windows to observe the action, to flagging internal links to trap
their usage to...you name it. My experience with most of these has not
been encouraging. Here's a novel approach from glenngv of
codingforums.com:

<html>
<head>
<title>Detecting Closing of Window in IE</title>
<script language="javascript">
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}
</script>
</head>
<body onunload="doUnload()">
<h3>Detecting Closing of Window in IE</h3>
Do the 3 ways below and see the difference:<br>
1. Try to refresh the page.<br>
2. Try to type any URL in the address bar.<br>
3. Try to close this window by clicking X button of this window.
</body>
</html>

Catches the mouseclick position. IE only, but easily modifiable. What
exactly did you need to do?

Jul 23 '05 #5
RobB wrote:
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}


The alert would also be shown, if a user types in another address and
hits enter (or navigates with keys) while his mouse is above and left
from the browser window.

This might be corrected by adding the screenX and screenY values. If the
resulting value is still negative, the window might be closed. But I
don't know if this must be the case as I couldn't find a reference who
describes the possible values of clientX/clientY for a closed window.

function doUnload() {
var e = window.event;
if (e.clientX+screen.width < 0 && e.clientY+screen.height < 0) {
alert("The window is closed...");
}
}

Daniel
Jul 23 '05 #6
Daniel Kirsch <Iw*****************@gmx.de> wrote in message news:<cn*************@news.t-online.com>...
RobB wrote:
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}
The alert would also be shown, if a user types in another address and
hits enter (or navigates with keys) while his mouse is above and left
from the browser window.

This might be corrected by adding the screenX and screenY values. If the
resulting value is still negative, the window might be closed. But I
don't know if this must be the case as I couldn't find a reference who
describes the possible values of clientX/clientY for a closed window.

function doUnload() {
var e = window.event;
if (e.clientX+screen.width < 0 && e.clientY+screen.height < 0) {
alert("The window is closed...");
}
}

Daniel


Daniel Kirsch wrote:
The alert would also be shown, if a user types in another address and
hits enter (or navigates with keys) while his mouse is above and left
from the browser window.


Interesting - that wasn't my experience: Event.clientX remained
positive except while clicking the 'x', then going wildly negative.
Not to worry: couldn't get this to work in Firefox; both Event.clientY
and Event.pageY remained set to zero regardless. Presumably IE's event
handling in this regard is non-standard (or buggy). Couldn't come up
with a viable solution.
Jul 23 '05 #7
> > > function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}


function doUnload() {
var e = window.event;
if (e.clientX+screen.width < 0 && e.clientY+screen.height < 0) {
alert("The window is closed...");
}
}


I don't get this! Does anyone's MSIE pops up anything when they click "x" in
the top left corner???

I can't seem to achieve anything like that!

What is wrong?

TIA

Tom
Jul 23 '05 #8
This comes from Tek-Tips:

http://www.tek-tips.com/viewthread.cfm?qid=924821

By adam0101:
In the onunload event of every page, open the survey at a position off
the screen. In the onload event of every page, close the survey. Then
put a timer in the survey window that repositions itself into view after
about 5 seconds. If another page from your site doesn't get loaded
within 5 seconds to close the survey, it'll display itself.

The tricky part is referencing the popup from the new page. This should
work:
CODE

<body onload="window.open('javascript:window.close()','p opupSurvey')"
onunload="window.open('survey.html','popupSurvey', 'left=-5000')">

In the survey:
CODE

<script>
setTimeout('moveTo(0,0)',5000);
</script>

Adam

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #9
On 26 Nov 2004 09:55:01 GMT, Theo Developer <th**@involveit.nl> wrote:

[snip]
<body onload="window.open('javascript:window.close()','p opupSurvey')"
onunload="window.open('survey.html','popupSurvey', 'left=-5000')">
Which will be blocked by every pop-up blocker in existence.
setTimeout('moveTo(0,0)',5000);


Decent browsers can also ignore that.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #10
Theo Developer wrote:
This comes from Tek-Tips:

http://www.tek-tips.com/viewthread.cfm?qid=924821

By adam0101:
In the onunload event of every page, open the survey at a position off
the screen. In the onload event of every page, close the survey. Then
put a timer in the survey window that repositions itself into view after
about 5 seconds. If another page from your site doesn't get loaded
within 5 seconds to close the survey, it'll display itself.

The tricky part is referencing the popup from the new page. This should
work:
CODE

<body onload="window.open('javascript:window.close()','p opupSurvey')"
onunload="window.open('survey.html','popupSurvey', 'left=-5000')">

In the survey:
CODE

<script>
setTimeout('moveTo(0,0)',5000);
</script>

Adam


Thank goodness nonsense like this doesn't come close to "working" (by any
definition) in my Web browser.

Thank goodness Microsoft has put features in place in Windows XP Service
Pack 2 to stop nonsense like this.

Thank goodness 3rd party vendors like Google and Yahoo and Norton offer
popup blockers that stop nonsense like this.

This advice might have actually worked on Web browsers 2 years ago. Today it
is not only bad functionality advice, it's bad technical advice, because it
simply WILL NOT WORK on a large number of Web browsers in use.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Christopher W. Douglas | last post: by
2 posts views Thread by Derek | last post: by
1 post views Thread by Chris Bruce | last post: by
1 post views Thread by Chirag Malvi | last post: by
5 posts views Thread by Stan Sainte-Rose | last post: by
5 posts views Thread by jimmy | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.