Solved

window unload event in Opera doesn't work

Posted on 2006-07-03
14
1,483 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:php121
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17034806
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
 
LVL 2

Author Comment

by:php121
ID: 17034866
Yea, I read that page when I was googling a workaround.  Any ideas for a workaround?
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17034876
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 2

Author Comment

by:php121
ID: 17034947
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
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17034968
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
 
LVL 2

Author Comment

by:php121
ID: 17034999
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
 
LVL 15

Expert Comment

by:deepaknet
ID: 17035137
Did you check out window.onBeforeUnload event?
0
 
LVL 2

Author Comment

by:php121
ID: 17035409
deepaknet, that only works in Internet Explorer which is worse than onUnload
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17035812
Workaround for a chat is using TWO windows.
One that checks the other window is closed and then reacts.

Michel
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17036910
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17037308
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
 
LVL 2

Author Comment

by:php121
ID: 17038968
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 17040496
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
 
LVL 2

Author Comment

by:php121
ID: 17040780
Thanks mplungjan, that works.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question