Javascript confirm redirect on page exit

Post Reply
User avatar
hiccup
Site Admin
Posts: 19
Joined: Sat Aug 01, 2015 8:44 pm

Javascript confirm redirect on page exit

Post by hiccup » Mon Sep 21, 2015 7:58 pm

Confirm on a page exit,
Redirect to another page.

Code: js Select all

window.onbeforeunload = function () {
    window.setTimeout(function () { // escape function context
        window.location = 'http://bbc.co.uk';
    }, 0);
    window.onbeforeunload = null;   // necessary to prevent infinite loop
                                    // that kills your browser
    return 'Press "Stay On Page" to go to BBC website!';
        // pressing leave will still leave, but the GET may be fired first anyway
}

Post Reply