Pls how can i redirect a page using Vanilla Javascript when the browser back button is clicked?

50 views Asked by At

This is what I came up with, but it doesn't seem to work.

function prev() {window.history.forward(); window.location.href = 'google.com';} 
setTimeout(prev(), 0); window.onunload = function() { null }
1

There are 1 answers

2
Mateusz Mółka On

I don't think you can, but you can warn users using window.onbeforeunload that they are gonna navigate back. This might not work in Opera though.