# \[WebGL\] catch close window button

**URL:** https://discussions.unity.com/t/webgl-catch-close-window-button/606731
**Category:** Web
**Created:** [November 26, 2015, 12:21pm UTC](https://discussions.unity.com/t/webgl-catch-close-window-button/606731 "2015-11-26T12:21:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Gushus](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@Gushus](https://discussions.unity.com/u/Gushus)
#### Post date: [November 26, 2015, 12:21pm UTC](https://discussions.unity.com/t/webgl-catch-close-window-button/606731/1 "2015-11-26T12:21:24Z")

</div>

Hej

Is there a way to catch the event of a clicked close-window button?  
I already tried:

```javascript
<script>window.onbeforeunload = function() { return "Sure?"; }</script>

```

On Top / Bottom of the ‘index.html’ - did’nt work  
`public void OnApplicationQuit() { Application.ExternalEval("..") }` - didn’t work

I would need some sort of a “Do you really want to leave?” Dialog if the user wants to close the popup where my WebGL content is running.

I’m looking forward for your answers as I’m already starting to get depressions… ^^

Have a nice day!  
Gushus

---

<div class="post-metadata">

### Author: ![NocturnalWisp](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/nocturnalwisp/32/369000_2.png) [@NocturnalWisp](https://discussions.unity.com/u/NocturnalWisp)
#### Post date: [November 28, 2021, 5:44pm UTC](https://discussions.unity.com/t/webgl-catch-close-window-button/606731/2 "2021-11-28T17:44:36Z")

</div>

Hey,

I know this question is very old, and I’m not sure if this solution works on that date or wasn’t implemented in the engine yet, but I figured that I would share my solution in case it help those coming across this question.

The way I solved this for work is as follows; I created a jslib file that can run Java Script and interface with unity using emscripten. The format for this file can be found here: [Unity - Manual: WebGL: Interacting with browser scripting (unity3d.com)](https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html)

I created a new function in the jslib called Initialize that I call within a monobehaviour. (See the same documentation for how to call the functions) I then handled the close window event. You can adjust this to how you like even setting a way to make the application “Dirty” and only showing the message when the user has changed something. Here is the function I made:

```javascript
    Initialize: function() {
        window.onbeforeunload = function() {
           return "Are you sure you really want to leave the application?";
        };
    },

```

I know that the javascript code block isn’t for this, but I used it anyway. 😛

Hopefully this helps the next person who is getting super annoyed with the webGL stuff.
