Forum Moderators: open

Message Too Old, No Replies

How do I detect when a user has closed a window?

         

WebDon

7:59 am on Dec 12, 2003 (gmt 0)

10+ Year Member



Before I get a bunch of notes about the evils of pop-ups let me first say that I agree and it's a specific request...the customer is always right, right?

What I need to do is open a small window but only when the original window is closed. What I have now is opening the small window whenever the current page is left, i.e. if they click on a link and go to another page the small window still comes up. I only want it to come up when the user closes the browser.

Does this make sense?

Thanks for your time!

dragonlady7

2:04 pm on Dec 12, 2003 (gmt 0)

10+ Year Member



A friend of mine stole code to do that from a porn site. I don't really surf for porn, so I don't know if they still do that. But some might, still... You never know.

WebDon

8:03 pm on Dec 12, 2003 (gmt 0)

10+ Year Member



Heh, thanks. I thought of that, just haven't resorted to it yet.

WebDon

7:24 pm on Dec 13, 2003 (gmt 0)

10+ Year Member



Well, after more research and experimentation I've decided that this just isn't possilbe in JavaScript.

I did finally find a couple of posts that had a similar work-around by adding some script to each link so that clicking on the links wouldn't trigger the new window, but that approach is very messy for this site (quite a few links) and it doesn't work if a user hits the forward or back buttons or uses the history bar to navigate.

If anyone has another idea I would love to hear it!

Thanks.

CloudLong

5:21 pm on Dec 25, 2003 (gmt 0)

10+ Year Member



Its far more than easy... to do that follow this instruction...

1. put this code below between your <head></head> tag.

<!--
Code By "CodeFreak" Cloud - codefreak.proboards19.com - 2003
You may leave this part of code intact to show your gratitude or remove it
-->
<script language=javascript>
function popitup()
{
window.open("poppedup.html", "popup", 'width=400,height=200,scrollbars=yes');
}
</script>

You can change the popup window attribute just as you wish. "poppedup.html" is your popup windows page, you can change it as you wish.

2. add this to your body tag

<body onunload="popitup();">

and you are done :)

WebDon

7:56 pm on Dec 26, 2003 (gmt 0)

10+ Year Member



Thanks Cloudlong.

I tried that approach earlier. It didn't achieve what exactly what I was after.

If I close the window it would certainly open the pop-up. However, it would also do it if I simply went to a new page.

Onunload will run the script whenever the document is unloaded and doesn't differentiate between when a window is closed or when a visitor simply leaves the page.

I needed the pop-up to fire ONLY when the original window was specifically closed and to NOT fire if the visitor just left the page.

CloudLong

2:38 pm on Dec 27, 2003 (gmt 0)

10+ Year Member



That is not possible, because the onunload will fire even if the user just change the url.

and there is no other way to detect whether if the user click the close button or simply change the url.

The only way I can think of is have a link or button that when the user click on it, the browser will close down, thus a pop up will appear e.g <a href=# onclick="popitup(); window.close();">Close the Window</a>. Other than that, I think it won't possible

WebDon

12:55 am on Dec 28, 2003 (gmt 0)

10+ Year Member



That's the conclusion I've come to: It's not possible.

Thanks for the help Cloudlong! It's always nice to have someone else verify what you find.

chrisadd

6:00 pm on Jan 22, 2004 (gmt 0)



It *is* possible! But it only works on IE. Here's how to do it.

<script type="text/javascript" language="javascript">

onunload = function() {
if ((window.event.clientX<0) && (window.event.clientY<0))
alert('Window was closed!');
}

</script>

WebDon

8:42 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Thanks Chrisadd. That project is done but I'm still curious so I'll give that a try.