S
Stevo
I've been using the unload event for a long time. I have this code,
which I've abstracted and made into a stripped down simple test case
below, and it works fine on the major browsers (IE5+, Firefox, Opera).
It also works for all Safari versions before 3.1.
It's as if they've deliberately made a change to prevent some actions in
the unload handler. Has anyone heard of such a restriction?
Here's the test case:
<html><body>
<a href="destination.html">click</a>
<script>
var started=new Date().getTime();
var imj;
function fin()
{
var diff=(new Date().getTime()-started)/1000;
imj=new Image();
imj.src="http://example.com/time.gif?"+diff;
// alert("fin");
}
if(window.addEventListener)
window.addEventListener('unload',fin,false);
else if(window.attachEvent)
window.attachEvent('onunload',fin);
</script>
<form><input type=button value="fin" onclick="fin()"></form>
</body></html>
Each time you click the fin form button, you see (via a http monitor
like Charles or Fiddler) that the image load occurs with the current
time delta since page load as a cache-busting query string. The file
gets a 404, but that doesn't matter, it's working fine when fin is
called from the form button.
If you instead click the link (or close the browser), fin is also called
(confirmable by uncommenting the alert), but the image load doesn't occur.
which I've abstracted and made into a stripped down simple test case
below, and it works fine on the major browsers (IE5+, Firefox, Opera).
It also works for all Safari versions before 3.1.
It's as if they've deliberately made a change to prevent some actions in
the unload handler. Has anyone heard of such a restriction?
Here's the test case:
<html><body>
<a href="destination.html">click</a>
<script>
var started=new Date().getTime();
var imj;
function fin()
{
var diff=(new Date().getTime()-started)/1000;
imj=new Image();
imj.src="http://example.com/time.gif?"+diff;
// alert("fin");
}
if(window.addEventListener)
window.addEventListener('unload',fin,false);
else if(window.attachEvent)
window.attachEvent('onunload',fin);
</script>
<form><input type=button value="fin" onclick="fin()"></form>
</body></html>
Each time you click the fin form button, you see (via a http monitor
like Charles or Fiddler) that the image load occurs with the current
time delta since page load as a cache-busting query string. The file
gets a 404, but that doesn't matter, it's working fine when fin is
called from the form button.
If you instead click the link (or close the browser), fin is also called
(confirmable by uncommenting the alert), but the image load doesn't occur.