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

T

Tom Szabo

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!!!
 
O

Oscar Monteiro

Tom Szabo said:
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')">
 
R

RobB

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?
 
T

Tom Szabo

RobB said:
"Oscar Monteiro" <[email protected]> wrote in message 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
 
T

Tom Szabo

Hi Rob,

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

No error with the script, just nothing.

Any clues?

TIA,

Tom


RobB said:
"Oscar Monteiro" <[email protected]> wrote in message 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?
 
D

Daniel Kirsch

RobB said:
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
 
R

RobB

Daniel Kirsch said:
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 said:
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.
 
T

Tom Szabo

function doUnload()
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
 
T

Theo Developer

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()','popupSurvey')"
onunload="window.open('survey.html','popupSurvey','left=-5000')">

In the survey:
CODE

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

Adam
 
M

Michael Winter

[snip]
<body onload="window.open('javascript:window.close()','popupSurvey')"
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
 
G

Grant Wagner

Theo said:
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()','popupSurvey')"
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top