使用$ location手动更改URL时保留上一页

问题描述 投票:0回答:2

我的控制器中有一个功能可以更改浏览器的URL,但是当用户点击后退按钮时我需要保留真实的上一页。

例如:

用户在/ home并导航到/ article1。当用户在页面中滚动时我更改URL,例如使用以下内容更改为/ article2:

$location.path('article2');

但是如果用户按下/ article2中的后退浏览器按钮,则url会更改为/ article1,我真的需要将导航重定向到/ home。

我用过这段代码:

var previousPage = document.referrer; // Get the previous location on page load
history.pushState({}, '', previousPage); // Alter the history adding the previous page

但它不起作用,因为在使用$ location更改URL时,历史状态会自动更改。

如何保存导航?

javascript angularjs browser-history
2个回答
0
投票

您还可以在本地存储上一页以返回。我有一个带有后退按钮的应用程序,允许您返回到您导航过的页面。尝试这样的事情:

sessionStorage.previousPage = "mypage";

function goBack(){
 $location.path(sessionStorage.previousPage);
};

0
投票

你可以用这种方式

window.onpopstate = () => history.replaceState(history.state, 'gotohome', '/')

它会工作但是,如何在不使用window.onpopstate的情况下更改上一页的url如果有人知道的话可以帮助我

© www.soinside.com 2019 - 2024. All rights reserved.