Search the blog

I had to run some logging via AJAX. You could run the AJAX call normally and then handle the response and then run the default link click behaviour. E.g. window.location = '/foo/bar';

However, this causes a bit of a delay to the user. In my case, I was just logging a user action so I didn’t need to handle the response. In such instances where you don’t need to do anything with the response and you want to guarentee the AJAX call is made, consider this code:

					
fetch('/ajax/foo/bar', {

    method: 'GET',
    keepalive: true

});					
				

Note the use of keepalive: true. This is the “magic” that makes the call run even when the browser is closed.

Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.