0

I am learning javascript and I was thinking about how can I add an alert or confirm box with javascript when a user wants to close the tab? it would be very good If you guys help me to do the job. Thank you in advance.

  • 4
    Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+alert+tab+close+site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Jan 10 '22 at 09:19

2 Answers2

0

window.onbeforeunload = function(e) {
    return 'Are you sure want to close the tab';
};
Aman Sharma
  • 933
  • 1
  • 4
  • 12
-2

alert("Hello! I am an alert box!!");