---
title: "Jquery Show Popup on window closing"  
description: "Jquery Show Popup on window closing"  
author: "Anonymous User"  
published: 2014-10-31  
updated: 2014-10-31  
canonical: https://www.mindstick.com/forum/2472/jquery-show-popup-on-window-closing  
category: "jquery"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# Jquery Show Popup on window closing

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to display a confirmation [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) when the user closes the [browser](https://www.mindstick.com/blog/155254/which-is-the-best-internet-browser)/tab and not when any of the [links](https://www.mindstick.com/blog/415/css3-links) on page is clicked.

I have got the first part of displaying the message on [window close](https://www.mindstick.com/forum/34365/refresh-parent-page-webgrid-on-window-close-in-mvc) working nicely but [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is that the message/alert is displayed also when [user clicks](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) on any link on the page.

I have got code from somewhere to prevent this [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) but still when ever any link is clicked the alert pops up.

## Here is the code:

```
<script src="js/jquery-1.7.1.min.js"></script><script src="js/jquery-ui.min.js"></script><script type="text/javascript" src="js/backfix-min.js"></script><script>    $(document).ready(function () {        $(window).bind('beforeunload', function (e) {            $("#lead-gen-modal").dialog("open");            return "Wait\! Don\'t Go\! We have a special offer for you\. Stay on this page to receive big savings\!";        });         $("a").click(function () {            window.onbeforeunload = null;        });    });</script>
```

## Replies

### Reply by Anonymous User

just use a global variable and set it to false when clicking a link.

```
var key = true;$(window).bind('beforeunload', function (e) {     if (key)        $("#lead-gen-modal").dialog("open");});
```

**update :**\

```
$(document).on("click", "a", function () {     key = false;});
```

or if you just want to prevent closing [window](https://www.mindstick.com/forum/161285/how-does-the-window-console-object-work-in-javascript) you can do this :\

```
window.onbeforeunload= function(e) {     if (key)         return false;}
```


---

Original Source: https://www.mindstick.com/forum/2472/jquery-show-popup-on-window-closing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
