简体   繁体   English

用户单击 JavaScript 中的浏览器后退按钮后如何将用户重定向到另一个页面

[英]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.

相关问题 限制用户登录后重定向到登录页面,即使他按角度按浏览器的后退按钮? - restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular? 请单击浏览器后退按钮时如何使用 Vanilla Javascript 重定向页面? - Pls how can i redirect a page using Vanilla Javascript when the browser back button is clicked? Javascript-如果单击“后退”按钮,如何重定向到主页 - Javascript - How to redirect to homepage if back button in clicked 使用javascript单击后退按钮时,重定向到另一个URL - Redirect to another url when back button is clicked using javascript 单击后退按钮时是否强制浏览器将用户带到页面的顶部? - Force the browser to take user to the TOP of the page when the back button is clicked? 如何使用JavaScript单击按钮将用户重定向到另一个页面? - How to redirect a user to another page with a button click using only JavaScript? 单击浏览器后退按钮时如何维护页面状态? - How to maintain the page state when browser back button is clicked? Beforeunload无法将用户重定向到另一个页面,如何在尝试关闭窗口/选项卡时将用户重定向到另一个页面/ URL? - Beforeunload is not working to redirect user to another page, how can I redirect user to another page/url when he attempts to close a window/tab? 用户使用 javascript 单击图像后如何启用按钮 - How to enable the button once the user clicked the image using javascript 在 React js 上单击按钮时如何重定向到另一个页面? - How to redirect to another page when the button is clicked on React js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM