Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery unload event

Tags:

jquery

Is there any way to determine if the user clicked on a link, the forward or back button or entered a new URL in the address bar, in the following event?

$(window).unload(function(e) {
    // here
}
like image 413
Mq_ Avatar asked Apr 24 '26 00:04

Mq_


1 Answers

Browsers can't distinguish between tab close, browser close, refresh, new address entered. All will trigger an unload event - the DOM is unloaded. Unfortunately there's no data passed to the unload event handler to distinguish between the different causes.

You could possibly do something with the mouse position - determine if the pointer is at the middle-top (for address bar), or right/left top for close. However its not a great solution as different browsers have their buttons in different positions, and Mac/PC have close buttons on opposite sides.

like image 150
reach4thelasers Avatar answered Apr 25 '26 14:04

reach4thelasers