Trigger event on page location change

I would like to know if there is a possibility to trigger an event when the user leaves the current page. More specifically, I would like to trigger a javascript Window confirm() method.

This could be useful in scenarios in which the user has unsaved changes, for example.

You can have a look at the onChange function in the Wix Location API.
https://www.wix.com/corvid/reference/wix-location.html#onChange

That should work suitably. Thank you very much.