Home ยป JavaScript ยป JavaScript: On Close Tab Show Warning Message

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 '?';


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.