onbeforeunload not/not correct working in Javascript

854 Views Asked by At

I'm trying to use the onbeforeunload or onunload event or even in a:

window.addEventListener('beforeunload', function(){alert("3");}, false); 

but it doesn't work, neither on Chrome nor on Firefox, the only method that works is the code below, but the problem is that it works at the start of loading and not when the page being closed, I spent more than 24 hours just with that, and I can't get it to work.

window.onbeforeunload  = closefct();
function closefct(e){
alert("5");
console.log("5");
return null
}
1

There are 1 best solutions below

0
Tasos Tsournos On

alert will be ignored by the browser. Use console.log to check if it is working properly.