4

If you try to send an Ajax request, a JSONP request, or even a window.name request on unload, Safari and Chrome run the code, but the server never sees the request. My theory is the thread of execution never allows the script tag to run before it changes the page. Here is a test page with the JSONP test. This code (and Ajax and window.name) creates a request for test.html in Firefox and IE7, but not Safari:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
    </head>
    <body>

        <script language="javascript" type="text/javascript">
            window.onunload = function(){
                var encode = "UTF-8";
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src= "/test.html";
                script.charset= encode;
                document.body.appendChild(script)
            }
        </script>

    </body>
</html>

Anyone know a good way to get around this? More specifically, anyone know of a way to force Safari to send a request on unload? The only solution I've found (which doesn't really help in my case) is synchronous XHR.

skaffman
  • 398,947
  • 96
  • 818
  • 769
moschel
  • 81
  • 1
  • 5
  • An what happens in firefox if the request takes 10 seconds to complete? Will firefox hang for 10 seconds? If so, I don't see the advantage of the script.src approach over a synchronous XHR. – pts May 15 '09 at 20:13

0 Answers0