如何在使用 document.location.replace 重定向后记住之前的 URL

How to remember previous URL after redirecting with document.location.replace

所以我正在试验小书签,我想通过单击小书签转到某个位置。请查看代码以更好地理解。

javascript:currentUrl=document.location.href;document.location.replace(currentUrl+'/images')

打开时使用此书签 google.com, 你将被重定向到 Google Images。 但是为什么它不记得前面的link, google.com? 浏览器上的后退按钮也变灰了...谢谢

如方法名称所示,replace() 将 replace/overwrite URL 并且不会保存到用户的浏览历史记录中。根据 MDN's documentation:

The Location.replace() method replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.

如果你想写入会话历史,你可以这样做: