0

I'm trying to confirm before user leaves the page and I used this solution

<script>
window.onbeforeunload = function(){
  return 'Are you sure you want to leave?';
};
</script>

but this only works when you interact with something on the page like maybe a clicking or typing, it doesn't work when you directly close the page

and I made a logging only popup page i.e. there is nothing on the page to interact with, so how do I still get it to confirm before leaving?

1 Answers1

0

In the docs

According to the specification, to show the confirmation dialog an event handler should call preventDefault() on the event.

So, you should at least do

window.onbeforeunload = function(e){
  e.preventDefault();
  return 'Are you sure you want to leave?';
};

However the docs also state

To combat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with, or may even not display them at all.

So you might be all out of luck with a completely empty page you have not interacted with.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • shame to know there's no solution, I'll mark your answer correct –  Nov 25 '21 at 09:58
  • 1
    @cakelover Yup, Sad to find that, there is no solution :) I was giving my try also on my local, unless we interact with the page, No solution yet for tab close or browser close !!! – Imran Rafiq Rather Nov 25 '21 at 10:25