beforeunload event in Webflow's Custom Code section to trigger a pop-up when users try to close the browser tab.To implement a pop-up that activates when a reader tries to close the browser tab or window in Webflow using JavaScript, you need to use the beforeunload event.
<script>
window.addEventListener('beforeunload', function (e) {
var confirmationMessage = 'Are you sure you want to leave this page?';
e.returnValue = confirmationMessage; // Standard for most browsers
return confirmationMessage; // Standard for Chrome
});
</script>
beforeunload event might not work consistently across all browsers and devices due to variations in handling.
To create a pop-up for when users try to close the tab, add a JavaScript beforeunload event via the Custom Code section of Webflow’s Project Settings. This will prompt users with a confirmation message, but test it for consistency across different browsers.