Press "Enter" to skip to content

JavaScript: On Close Tab Show Warning Message

Here I am giving an example of JavaScript code which you can use to show a warning message on the close tab.

Before using the below code, I have tried many examples of JavaScript code which I found on Google, but those didn’t work.

But this piece of code works perfectly, and now, even I forgot from where I picked 🤔.

On Close Tab Show Warning Message Using JavaScript

Use the following JavaScript code between the head tags:

<script type="text/javascript">
   window.onbeforeunload = function (e) {
      e = e || window.event;

      if (e) {
         e.returnValue = '?';
      }

      // For Safari
      return '?';

   };
</script>

Instead of a '?' mark for the return message value, you can use any warning message text, but it always gives the default Chrome or Safari warning message.

If you want to use this feature in Oracle Apex, check the following post, JavaScript: on close tab show warning message.

Have you found the answer to your question? If not, you can discuss it with me in the comments section below or join my Q&A community OrclQA.com for developers and ask your question. It is FREE.

Vinish Kapoor

Follow

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
João Coimbra
João Coimbra
4 months ago

How about hiding ir disabling the X upper right hand side button on a modal page? I used the css display on the class but it leaves a glimpsy of the button before it is definitively hidden?