Skip to content
Advertisement

The script is not working in Mozilla Firefox

I need to send some data to the server when the page is closed or refreshed. Therefore, I created a simple script that can handle my needs. The problem is that this script is not working in Mozilla firefox.

The script is working in many other browsers like chrome, chromium, brave, opera, falkon, epiphany, qutebroser, Midori, safari, edge. The problem is only with firefox.

JavaScript

The error message is: Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.

EDIT: if the event for onbeforeunload is registred as here:

JavaScript

it is working, but I need to confirm that I want to leave the page.

enter image description here

As I found on the internet, the problem arises because firefox uses its own implementation of fetch.

———————–SOLUTION [still not working in firefox correctly]————————-

JavaScript

———————–SOLUTION————————-

I used sendBeacon instead of fetch

so the final code is following:

JavaScript

Advertisement

Answer

Let’s add a bit more code to see what is going on, allow the fetch to complete then process any error (pause to see it) then proceed to the unload if no error happened – our desired case.

JavaScript
Advertisement