Ajax (or JSONP) when unloading using Safari / Chrome

If you try to send an Ajax request, a JSONP request, or even a window.name request when unloading, Safari and Chrome run the code, but the server will never see the request. My theory is a thread of execution that never allows a script tag to run before a page changes. Here is a test page with a JSONP test. This code (both 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>

Does anyone know a good way around this? In particular, does anyone know how to get Safari to submit a download request? The only solution I found (which doesn't really help in my case) is synchronous XHR.

+3

Source: https://habr.com/ru/post/1708452/


All Articles