{"id":858,"date":"2014-06-11T18:45:35","date_gmt":"2014-06-11T16:45:35","guid":{"rendered":"http:\/\/blog.visisoft.de\/yalst\/?p=858"},"modified":"2015-05-28T22:59:49","modified_gmt":"2015-05-28T20:59:49","slug":"ajax-on-unloading-a-web-page","status":"publish","type":"post","link":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/","title":{"rendered":"AJAX on web page unloading"},"content":{"rendered":"<div  style='padding-bottom:10px; ' class='av-special-heading av-special-heading-h4    avia-builder-el-0  el_before_av_codeblock  avia-builder-el-first  '><h4 class='av-special-heading-tag '  itemprop=\"headline\"  >AJAX on web page unloading<\/h4><div class='special-heading-border'><div class='special-heading-inner-border' ><\/div><\/div><\/div>\n\n<section class=\"av_textblock_section \"  itemscope=\"itemscope\" itemtype=\"https:\/\/schema.org\/BlogPosting\" itemprop=\"blogPost\" ><div class='avia_textblock  '   itemprop=\"text\" ><p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-859\" style=\"float: left; margin-right: 10px; margin-bottom: 10px;\" src=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/64px-JavaScript-logo.png\" alt=\"JavaScript-logo\" width=\"64\" height=\"64\" \/><\/p>\n<p><strong>TL;DR <\/strong>On page <em>unload<\/em>\u00a0AJAX network requests are sent by all browsers if &#8211; contrary to the general advice &#8211; the <em>synchronous<\/em> flavour of \u00a0<code>XMLHttpRequest<\/code>\u00a0(SJAX) is used.<\/p>\n<h4>\u00a0The Problem of sending goodbye data on page unload<\/h4>\n<p>occurs often that a single page web application (SPA) needs to change the state in its cloud end (clean up resources, close network connections&#8230;) when a user quits the application (i.e. she closes the browser window).<br \/>\nThe obvious choice is to subscribe for the DOM window&#8217;s <code>unload<\/code> and\/or <code>beforeunload<\/code> events with handler methods and trigger a remote signal on a server via <code>XMLHttpRequest<\/code> (i.e. AJAX).\u00a0The result of that signalling is usually of no interest to the SPA front end (browser page).<br \/>\nHowever what can be done in these handlers has always depended on the various browser implementations.<\/p>\n<h4>The beforeunload Event Handler<\/h4>\n<p>can prompt the user to remain in the SPA showing a native UI dialog while JavaScript execution is halted. For creating network requests the same constraints as with the <code>unload<\/code> handler apply, except for the Chrome browser.<\/p>\n<h4>In the unload Event Handler<\/h4>\n<p>the UI is already gone. Therefore UI operations (alert, window.open,&#8230;) do not make sense here.\u00a0How much JavaScript execution time remains is unknown. However to deduct that operations in here have to be <em>fast<\/em> and avoid all blocking JavaScript instructions all together is <em>wrong<\/em>!<br \/>\nTests (see the table below) show that many browsers do not issue network requests if done asynchronously.<\/p>\n<h4>The Solution<\/h4>\n<p>is to use <em>AJAX<\/em> without the first <em>&#8216;A&#8217;<\/em> or <em>S(ynchronous)JAX<\/em> if there were such a thing. Thus simply setting the <code>async<\/code> flag of the <code>XMLHttpRequest.open<\/code> method to <code>false<\/code>\u00a0ensures the network request is fired by all browsers. But never mind about that <em>blockingness<\/em> of the JavaScript instruction, because we are not interested in the response anyway!<\/p>\n<h4>A Test Case<\/h4>\n<p>is kept in <a title=\"jquery\" href=\"http:\/\/jquery.com\">jQuery<\/a>\u00a0and can be found under\u00a0<a title=\"onunload.html\" href=\"https:\/\/github.com\/jquery\/jquery\/blob\/master\/test\/data\/ajax\/onunload.html\" target=\"_blank\" rel=\"noopener\">jquery\/test\/data\/ajax\/onunload.html<\/a><\/p>\n<h4>Test Results<\/h4>\n<\/div><\/section>\n<div class='avia-data-table-wrap avia_responsive_table'><table class='avia-table avia-data-table avia-table-1  avia-builder-el-3  el_after_av_textblock  avia-builder-el-last  avia_pricing_default '  itemscope=\"itemscope\" itemtype=\"https:\/\/schema.org\/Table\" ><tbody><tr class='avia-heading-row'><th class=''><\/th><th class='avia-center-col'><img decoding=\"async\" src=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/IE.png\" alt=\"\" \/><\/th><th class='avia-center-col'><img decoding=\"async\" src=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/Chrome.png\" alt=\"\" \/><\/th><th class='avia-center-col'><img decoding=\"async\" src=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/Firefox.png\" alt=\"\" \/><\/th><th class='avia-center-col'><img decoding=\"async\" src=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/Safari.png\" alt=\"\" \/><\/th><\/tr><tr class='avia-heading-row'><th class=''><\/th><th class='avia-center-col'>v.10<\/th><th class='avia-center-col'>v.36<\/th><th class='avia-center-col'>v.30<\/th><th class='avia-center-col'>v.7<\/th><\/tr><tr class=''><td class=''>AJAX (async = true)<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>fails<\/td><td class='avia-center-col'>?<\/td><\/tr><tr class=''><td class=''>AJAX (async = true)<br \/>\nafter dismissed a beforeunload prompt<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>fails sometimes<\/td><td class='avia-center-col'>fails<\/td><td class='avia-center-col'>?<\/td><\/tr><tr class=''><td class=''>SJAX (async = false)<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>ok<\/td><td class='avia-center-col'>ok<\/td><\/tr><\/tbody><\/table><\/div><style type='text\/css'>.avia-table-1 td:nth-of-type(1):before { content: ''; } .avia-table-1 td:nth-of-type(2):before { content: ''; } .avia-table-1 td:nth-of-type(3):before { content: ''; } .avia-table-1 td:nth-of-type(4):before { content: ''; } .avia-table-1 td:nth-of-type(5):before { content: ''; } <\/style>\n","protected":false},"excerpt":{"rendered":"<p>On page unload AJAX network requests are sent by all browsers if &#8211; contrary to the general advice &#8211; the synchronous flavour of  XMLHttpRequest (SJAX) is used.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[67],"tags":[91,68,104,105],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk<\/title>\n<meta name=\"description\" content=\"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk\" \/>\n<meta property=\"og:description\" content=\"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/\" \/>\n<meta property=\"og:site_name\" content=\"yalst - Live Support | Live Chat | Live Help | Help Desk\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-11T16:45:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-05-28T20:59:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/64px-JavaScript-logo.png\" \/>\n<meta name=\"author\" content=\"Matthias Seemann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Matthias Seemann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/\",\"url\":\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/\",\"name\":\"AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk\",\"isPartOf\":{\"@id\":\"https:\/\/www.yalst.de\/en\/#website\"},\"datePublished\":\"2014-06-11T16:45:35+00:00\",\"dateModified\":\"2015-05-28T20:59:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.yalst.de\/en\/#\/schema\/person\/32edfd67d7dc13e809181afe316fcccb\"},\"description\":\"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.yalst.de\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AJAX on web page unloading\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.yalst.de\/en\/#website\",\"url\":\"https:\/\/www.yalst.de\/en\/\",\"name\":\"yalst - Live Support | Live Chat | Live Help | Help Desk\",\"description\":\"Ihr umfassender Live Support Chat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.yalst.de\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.yalst.de\/en\/#\/schema\/person\/32edfd67d7dc13e809181afe316fcccb\",\"name\":\"Matthias Seemann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.yalst.de\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6d883a962ac315cf385799a15783f440?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6d883a962ac315cf385799a15783f440?s=96&d=mm&r=g\",\"caption\":\"Matthias Seemann\"},\"description\":\"Entwickler und Mitglied der Gesch\u00e4ftsf\u00fchrung bei der Visisoft OHG\",\"url\":\"https:\/\/www.yalst.de\/en\/author\/seemannvisisoft-de\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk","description":"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/","og_locale":"en_US","og_type":"article","og_title":"AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk","og_description":"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.","og_url":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/","og_site_name":"yalst - Live Support | Live Chat | Live Help | Help Desk","article_published_time":"2014-06-11T16:45:35+00:00","article_modified_time":"2015-05-28T20:59:49+00:00","og_image":[{"url":"https:\/\/www.yalst.de\/wp-content\/uploads\/2015\/04\/64px-JavaScript-logo.png"}],"author":"Matthias Seemann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Matthias Seemann","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/","url":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/","name":"AJAX on web page unloading - yalst - Live Support | Live Chat | Live Help | Help Desk","isPartOf":{"@id":"https:\/\/www.yalst.de\/en\/#website"},"datePublished":"2014-06-11T16:45:35+00:00","dateModified":"2015-05-28T20:59:49+00:00","author":{"@id":"https:\/\/www.yalst.de\/en\/#\/schema\/person\/32edfd67d7dc13e809181afe316fcccb"},"description":"On page unload AJAX network requests are sent by all browsers if - contrary to the general advice - the synchronous AJAX (SJAX) is used.","breadcrumb":{"@id":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.yalst.de\/en\/ajax-on-unloading-a-web-page\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.yalst.de\/en\/"},{"@type":"ListItem","position":2,"name":"AJAX on web page unloading"}]},{"@type":"WebSite","@id":"https:\/\/www.yalst.de\/en\/#website","url":"https:\/\/www.yalst.de\/en\/","name":"yalst - Live Support | Live Chat | Live Help | Help Desk","description":"Ihr umfassender Live Support Chat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.yalst.de\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.yalst.de\/en\/#\/schema\/person\/32edfd67d7dc13e809181afe316fcccb","name":"Matthias Seemann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.yalst.de\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6d883a962ac315cf385799a15783f440?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6d883a962ac315cf385799a15783f440?s=96&d=mm&r=g","caption":"Matthias Seemann"},"description":"Entwickler und Mitglied der Gesch\u00e4ftsf\u00fchrung bei der Visisoft OHG","url":"https:\/\/www.yalst.de\/en\/author\/seemannvisisoft-de\/"}]}},"_links":{"self":[{"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/posts\/858"}],"collection":[{"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":18,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":4220,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/posts\/858\/revisions\/4220"}],"wp:attachment":[{"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yalst.de\/en\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}