Page 1 of 2

OnClose Event

Posted: Sat May 27, 2006 8:27 pm
by phppage
Could any one advise me if OnClose is a event handler in JS. Have seen it quoted on some sites but can not find it in http://www.w3schools.com

Posted: Sat May 27, 2006 9:07 pm
by hawleyjr
I think you're looking for:

onUnload not onClose

Posted: Sun May 28, 2006 6:16 am
by Chris Corbyn
hawleyjr wrote:I think you're looking for:

onUnload not onClose
And depending upon what you need to do with most modern browsers are very restrictive over it. onbeforeunload, although non-standard works in FF and IE but the main purpose is for generating warning when a user is in the middle of something and tries to close the window.

Posted: Sun May 28, 2006 6:55 am
by phppage
I tried onunload event but event was triggered everytime the user click a link on the website rather than closed it. What I want is for the parent window to focus when the child is closed.

Posted: Sun May 28, 2006 7:43 am
by Chris Corbyn
phppage wrote:I tried onunload event but event was triggered everytime the user click a link on the website rather than closed it. What I want is for the parent window to focus when the child is closed.
I bet that's one of those things that browsers will deny you access to using onunload.... could you imagine a cascade of annoying popups all bringing each other back into focus? It will require some use input I bet.

Code: Select all

<script type="text/javascript">
<!--
function closeAndFocus()
{
    if (window.opener) window.opener.focus();
    window.close();
}
// -->
</script>

<a href="javascript:closeAndFocus();">Click here to close this window and return to your shopping cart</a>

Posted: Sun May 28, 2006 8:32 am
by phppage
Do have that option in there already but was wondering if it would be possible to do it when the user just closes the browser window through the normal means.

Posted: Sun May 28, 2006 8:35 am
by feyd
it's possible to detect if the window is being closed. onunload can be used for that purpose.

Posted: Sun May 28, 2006 7:55 pm
by phppage
feyd wrote:it's possible to detect if the window is being closed. onunload can be used for that purpose.
How would you go about differentiate between the window being closed and a link being selected by the user within that same window?

Tried it this way

Code: Select all

<body onunload="opener.focus" onload="self.focus">
But everytime a link was selected within the child window the parent would focus then once the child had loaded that would then come back into focus. Think this would annoy the users after a while :lol:

Posted: Sun May 28, 2006 11:11 pm
by feyd
when the window is being closed one or more of the window references are destroyed when onunload is called. I don't remember which window I used to test.

Posted: Mon May 29, 2006 4:10 am
by Chris Corbyn
NULL out the unload event if they are clicking a hyperlink in the page. This will disable it.

Posted: Mon May 29, 2006 6:14 am
by phppage
Do you mean I would have to do a onclick event for every link on the page?

Posted: Mon May 29, 2006 6:35 am
by Chris Corbyn
phppage wrote:Do you mean I would have to do a onclick event for every link on the page?
That was what I was referring to.... it's ok you son't need to hard-code that into each hyperlink, you can javascript to do that hard work for you ;)

Posted: Mon May 29, 2006 7:50 am
by phppage
Not to take up too much more of your time, in general terms how would you go about getting JS to do that? Have given it some thought and investigation but can not find a solution :?

Posted: Mon May 29, 2006 11:59 am
by Chris Corbyn
Something along the lines of:

Code: Select all

<script type="text/javascript">
<!--

function nullEvent()
{
    window.onunload = false;
}

var links = document.getElementsByTagName('a');

for (var i in links)
{
    if (links[i].captureEvent) links[i].captureEvent(CLICK);
    
    links[i].onclick = nullEvent;
}

// -->
</script>
That's pretty pseudo although it may just work - I haven't tested it.

Posted: Mon May 29, 2006 12:48 pm
by phppage
Thanks for that, will give it a go and let you know :D