OnClose Event

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

OnClose Event

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I think you're looking for:

onUnload not onClose
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's possible to detect if the window is being closed. onunload can be used for that purpose.
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

NULL out the unload event if they are clicking a hyperlink in the page. This will disable it.
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post by phppage »

Do you mean I would have to do a onclick event for every link on the page?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post 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 :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post by phppage »

Thanks for that, will give it a go and let you know :D
Post Reply