Script for browser back button redirect

Bradley Penn

Registered Member
Joined
Dec 8, 2019
Messages
61
Reaction score
26
I am looking for a script that will redirect a site visitor to a page that I set when they click the back button in their browser, instead of going to the page they were on previously.

I searched the forum and web but couldn't find a working code. Any help would be appreciated. Thank you.
 
Congrats! Do any of them work?
This does, tested..

Code:
window.history.pushState({page: 1}, "", "");

window.onpopstate = function(event) {
    if(event){
        window.location.href = 'https://www.google.com/';
        // Code to handle back button or prevent from navigation
    }
}
 
Back
Top