Show Alert when Visitor Leaving a Web Page

Here is how to show an alert message when a visitor leaving a web page

With this code, we can show a message or dialogue of your choice when a visitor to your website wanting to leave. In PHP, i guess this is not possible so we can use this simple JavaScript.

This piece of code can also be used between your desired functions

JavaScript Code to Show Alert When User leaving

<script type="text/javascript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
    return "Are you sure you want leave?";


Post navigation


This site uses Akismet to reduce spam. Learn how your comment data is processed.