The on unload event is too late for you to be able to achieve anything. I think that it it the onbeforeunload event that you are wanting to use.
It is not possible to determine the reason wh the page is loading. It could be because the page is being closed, or refreshed, or a link is being loaded, or they are navigating to another part of your site.
A possibly useful technique could be to monitor the onclick event for when they click on a link to somewhere that is not on your site. If it's to some other site, you can raise a flag that indicates as such, so that the onbeforeunload function can then check that flag and take action if that's the case.