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


Related Posts

6 Web Design Secrets Every Business Owner Should Know

Photo Credit: Tranmautritam via Pexels 6 Web Design Secrets Every Business Owner Should Know According to industry experts, small businesses need to have a website to attract…

PHP: Get Visitors IP and Location and Save to TXT File

How to get visitors IP address and Location and save them to Text file in php This simple php code can be used to save visitors data…

Hide go to Top Button in CSS

How to hide go to top button in CSS The go to top button can be used to go to the top section of your website quickly….

How to Make a Text Blink in CSS

How to Animate or Blink a Text in CSS Blinking texts can capture user attention easily so you can use it for texts that needs more attention…

Change SVG Background Color in CSS

How to Change SVG Color with CSS Properties Hidden content goes hereTo change svg color background, i tried some available methods, but none worked except this code,…

Make your Website for Lowest Price

Create a Website for your Brand or for you Easier and Cost Effective Do you need a website? Looking to create a website? Want hire someone who…

Leave a Reply

Your email address will not be published. Required fields are marked *