Reputation: 46
Below is my code:
window.addEventListener("beforeunload", function(event) { <br/>
event.preventDefault(); <br/>
event.returnValue = ''; <br/>
window.location.href = 'https://google.co.in'; <br/>
return false; <br/>
});
Upvotes: 1
Views: 290
Reputation: 1966
There is an error in window.location.href('https://google.co.in')
just replace this with window.location.href = 'https://google.co.in';
it will work. location.href is used like this given an link and another method is given below.
window.location.replace("http://www.google.com");
Events that trigger the beforeunload
function:
This code prevent the back button and replace the window to google.com
window.addEventListener( "pageshow", function ( event ) {
var historyTraversal = event.persisted ||
( typeof window.performance != "undefined" &&
window.performance.navigation.type === 2 );
if ( historyTraversal ) {
// Handle page restore.
window.location.replace("http://www.google.com");
}
});
Upvotes: 0
Reputation: 326
You can try to modify history, and use state to know if user go back
window.onpopstate = function(event) {
if (event.state && event.state.redirect) {
window.location.replace("http://www.google.com");
}
};
history.replaceState({redirect: true}, "");
history.pushState({redirect: false}, "");
Upvotes: 1