I want to implement a custom confirmation pop-up (that does not look like a JS alert) when user tries to close the tab or window. I have checked many similar questions already and all the solutions are really old, where customizing the text on this pop-up is now deprecated for all the modern browsers.
I have below code working with default JS alert type message, but is it anyway possible to customize this? Please suggest.
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<title>Leave Site Pop-up</title>
<body>
<h1>My First Heading</h1>
<input name="name" id="name"/>
<script type="text/javascript">
const beforeUnloadListener = (event) => {
event.preventDefault();
return event.returnValue = "Are you sure you want to exit?";
};
const nameInput = document.querySelector("#name");
nameInput.addEventListener("input", (event) => {
if (event.target.value !== "") {
addEventListener("beforeunload", beforeUnloadListener, {capture: true});
} else {
removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
}
});
</script>
</body>
</html>