window unload event in Opera doesn't work

Hi,

I need to catch the window.unload event in all browsers.  It works in IE, Mozilla, and Firefox, but not in Opera.  

Is there a workaround to this?

This is an urgent question, so hence the large number of points.

Thanks
LVL 2
php121Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
FOr example like this

win = null;
monitorId = "";
function testWin() {  
  if (!win) return
  if (win && !win.closed()) return
  if (win.closed) tellUser()
// OR
  var t = new Date.getTime()
  var min = 60000; // one minute
  if (win && (t-win.timestamp) < min) tellUser(); // you can do timestamp = new Date.getTime() in the chat window..
 
}
function openchat() {
  win = window.open('chat.html','newwin')
  monitorId = setInterval('testWin()',400)
}


and
0
 
DireOrbAntCommented:
http://www.quirksmode.org/js/events_compinfo.html

Look under unload it says:

In Opera load and unload do not fire when the user uses Back or Forward to enter or leave a page, or when the user closes the window.
This severely hampers the usefulness of the events. When I write a load or unload script to, for instance, adjust the navigation of this site, I want it to run whenever the user enters or leaves the page, and not just when a browser thinks it's appropriate.
Therefore I judge Opera's support Minimal and not Incomplete.
0
 
php121Author Commented:
Yea, I read that page when I was googling a workaround.  Any ideas for a workaround?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DireOrbAntCommented:
Relying on a client-side unload event is not great in the first place.  Can you give us more detail on what you are trying to do?
0
 
php121Author Commented:
I am working on a PHP Instant Messenger, and when the chat window is closed, I want the option of emailing the chat log to the user.  
0
 
DireOrbAntCommented:
Is the whole system stateless? Hit to a php page and that's it?
If it's not, then you probably have some kind of "Service" running and you can detect disconnects and then send the log.

If it is, you can't rely on it (unload event). What if their internet connection drops?
0
 
php121Author Commented:
Yea, there is a kind of "service", which is in the form of a contact list that uses AJAX to check for updates to the state of the contact list.  Currently, when a chat is deemed inactive (2 minutes of it being empty - as people can rejoin if they get disconnected from the net), then it sends out the transcripts automatically if the admin has selected this option.  

However, if all users close their contact lists, then there is no "service" running to run a check and send out the emails.  That's why I would like to use javascript to catch the closing of the chat window and then do the transcript emailing at that point instead of waiting for 2 minutes and hoping at least one contact list is open to run the check.

You can check out the system at http://www.php121.com - there is a demo on the front page
0
 
deepaknetCommented:
Did you check out window.onBeforeUnload event?
0
 
php121Author Commented:
deepaknet, that only works in Internet Explorer which is worse than onUnload
0
 
Michel PlungjanIT ExpertCommented:
Workaround for a chat is using TWO windows.
One that checks the other window is closed and then reacts.

Michel
0
 
kaliyugkaarjunCommented:
0
 
DireOrbAntCommented:
This still doesn't solve the fact that either of these are not reliable solutions. Is it possible in your system to call a php script on a timer? Like the AT command on Windows? You could run one every say 5 minutes and make your check there. If you make your check server-side, it will be 100% reliable.
0
 
php121Author Commented:
Hmm, I will try catching the window close event from the main contact list as per what mplungjan said.  I'll update this question when I try it out.
0
 
php121Author Commented:
Thanks mplungjan, that works.
0
All Courses

From novice to tech pro — start learning today.