Solved

window unload event in Opera doesn't work

Posted on 2006-07-03
14
1,479 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
Comment Utility
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
Comment Utility
Yea, I read that page when I was googling a workaround.  Any ideas for a workaround?
0
 
LVL 26

Expert Comment

by:DireOrbAnt
Comment Utility
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
 
LVL 2

Author Comment

by:php121
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you check out window.onBeforeUnload event?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Author Comment

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

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
0
 
LVL 26

Expert Comment

by:DireOrbAnt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks mplungjan, that works.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now