Popup before closing window

Popup before closing window if you're just navigating across pages in the same domain it won't tigger a pop up message. Currently the custom message is now working so I'm researching why
 avatar
unknown
javascript
5 months ago
769 B
5
Indexable
window.onbeforeunload = function (e) {
    // Get the current domain
    var currentDomain = window.location.hostname;
    
    // Get the element the user clicked (if it's a link) or if the window is closing
    var newUrl = document.activeElement.href; 
    
    // If the user is navigating to a new URL and it contains the same domain, don't prompt
    if (newUrl && newUrl.indexOf(currentDomain) !== -1) {
        return; // No prompt, within the same domain
    }

    // Otherwise, show a prompt if they are leaving the site or closing the tab
    var message = "Are you sure you want to leave this page?";
    e = e || window.event;

    // For older browsers
    if (e) {
        e.returnValue = message;
    }

    // For newer browsers
    return message;
};
Editor is loading...
Leave a Comment