webdev-power-kit
Version:
A powerful toolkit that simplifies access to browser features like clipboard, notifications, battery, vibration, and more — perfect for modern web developers.
28 lines (27 loc) • 851 B
JavaScript
let listenerAttached = false;
let currentMessage = "Are you sure you want to leave? Changes may not be saved.";
/**
* Enables the tab close or refresh warning popup.
* @param message Optional custom message for the prompt.
*/
export function enablePreventClose(message) {
if (listenerAttached)
return;
listenerAttached = true;
if (message)
currentMessage = message;
window.addEventListener("beforeunload", beforeUnloadHandler);
}
function beforeUnloadHandler(e) {
e.preventDefault();
e.returnValue = currentMessage; // Most browsers use this for legacy
}
/**
* Disables the warning popup and allows normal tab closing.
*/
export function disablePreventClose() {
if (!listenerAttached)
return;
window.removeEventListener("beforeunload", beforeUnloadHandler);
listenerAttached = false;
}