Main

Sure you want to leave? - (The Standard, Ep. 14)

In this episode, Sam explains how to use the beforeunload event to ask users whether they're sure they want to leave your page, and how to decide when you should use it. Read the longer article → http://bit.ly/2D3IffU Watch more videos from the series → http://bit.ly/2OHyt42 Remember to subscribe to the Chrome Developers Channel for more updates → http://bit.ly/ChromeDevs1

Chrome for Developers

5 years ago

hey everyone it's Sam here sometimes your users will leave your page but what if they're still in the middle of an operation like submitting a form or uploading a file in this episode I'm going to show you the standard way to Warner user that they're closing your page let's begin [Music] when a user closes your page or follows a link to a different page which is kind of like losing your page your JavaScript has a chance to intercept this and warn your user via the before unload event let's take
a look so here is the simplest example we can add a listener in the before unload event and return a message you have to give a message because all browsers will display at the batum but newer browsers will actually override this to a very generic one about changes may not be saved either way this will prompt the user are you sure you want to close me but we know that a page which always warns you it's going to be closed it's going to be incredibly frustrating for your users so you should add a
condition to only pop up the dialog when you might lose some data like a network request is still in transit or a form is unsubmitted what I suggest is to use unfinished promises for example every time you do a network fetch you get back a promise like this we can write a method which adds that promise to a set of pending operations if anything is in that set we still have operations were waiting for so we can make sure to block the page from unloading then we had a method to the promise for whe
n it's done which removes it from the set the same set could control the display of a busy spinner this is a really neat way to remind your users that something isn't working and that they'll learn that closing a page during that time will prompt them to wait I also want to cover a special case modern browsers have a method called send beacon which lets you send a network request without waiting for a response you file a request and forget about it it's really useful for analytics events and if
it suits your needs you won't need to block your users until it completes it's actually designed to run even after a page is closed be sure to check out the extended article where we explain this topic in a bit more depth and we have some code snippets for you to use so remember use the before unload event to prompt the user use the set to keep track of unfinished business on your page and you send bacon for fire-and-forget Network requests that was unloading the page the standard way see you on
the next hit we've got my process right it's very important

Comments

@ELStalky

Favourite feature of cancerous web sites.

@legoman777777777

Thanks, Sam! Didn't know about beacons, very good content

@mariokrstevski8836

Could I change some other things in between the code? For example, grab some values from the dom elements and send that value somewhere. Or maybe activate an animation on the screen, and the animation would appear while the dialog box exists ( the user still hasn't clicked anything )

@Falconcress

How to disable this feature for all website?

@vinayc

Great playlist, you guys should make a video on CSP.

@zibtihaj3213

before I used to close Firefox it always used to ask me YOU SURE YOU WANT TO CLOSE ALL TABS... something like that and now it suddenly stops asking me ... what is going on, who do I get that back ? I think it is some option on the Firefox page. Pls advise

@surendragolla3136

great!!,, but still confusion on how can i handle buttons 'leave' and 'cancel' in the popup, is it possible to handle these buttons as a developer .. thanks in advance..

@chriszertamayo2071

please add another event or feature for closing the tab, i want my logout() or api call to be triggered by closing tab only and not on page refresh

@eez

never used beacons before, good learning. thanks

@rajuputtaswamaiah3049

Can we show the custom message?

@omri9325

Today I had to close a mobile web page with 4 popup dialogs for cookies, registration etc, and another one for chrome suggesting to change to reading mode. What the web has become...

@souravkarmakar919

Hello team, I'm trying to call one api on this beforeunload. Browser is not waiting for the response. Can we cancel this event? If yes then we can cancel this event after calling the api and will call window.close() after the api response. Can anyone please help me how we can cancel this event? I tried lots of solution and also try event.stopPropagation(). It's not working.

@prithviraju6289

Can I customise the modal. I know it's not possible. I have tried popstate btw. It triggers on back and farward only. Is there any other way???

@sharadshinde9101

How can Call to unload event while closing chrome

@ajones1274

Anyone got any tutorials or info on promise sets ? Currently developing a vuejs application and never thought of doing this and sounds like a great thing to implement

@JuanVargas-fn9fn

why cant we change the default message even when changing the returnValue?

@rajveersinghgangwar

It's nice. Feeling proud for adding 1st comment

@jainimsomaiya7399

On closing the browser I want to get prompted and call the logout function whereas on refreshing the browser I just want to get prompted. How do I do?

@TheNewton

Features like this should be a whitelisted security configuration|step: Unknown sites shouldn't be able to freeze user action at the app lvl we dealt with that bull for decades. This feature is also a SaSS crutch - So what if I close the tab! current progress should just be saved&versioned. "Prompts" are a slap on UX feature to avoid implementing a healthier more robust save system that doesn't care if the tab is closed or crashes.