[英]How to redirect user to another page once he has clicked the browser back button in JavaScript
What I am trying to achieve is to redirect the user to yahoo's page once he has clicked the browser's back button.我想要实现的是在用户单击浏览器的后退按钮后将用户重定向到雅虎的页面。 Here is my attempt, currently nothing happens:
这是我的尝试,目前没有任何反应:
window.addEventListener('popstate', function() {
var lastVisitedUrl = window.history.go(-1);
window.history.replaceState(lastVisitedUrl , "Sample Title", "/yahoo.com");
window.location.href.back()
});
How can I make the redirection once the browser back button is clicked?单击浏览器后退按钮后,如何进行重定向?
Try this works properly试试这个工作正常
window.history.pushState({page: 1}, "", "");
window.onpopstate = function(event) {
if(event){
window.location.href = 'https://www.yahoo.com';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.