【发布时间】:2020-09-07 16:31:40
【问题描述】:
对于一个网站,当用户点击一个按钮时,它会将他们带到 http://localhost:8080/differentpage。我想要发生的是,当他们在浏览器中单击后退按钮时,它会重新加载 http://localhost:8080。
使用下面的脚本,如果我离开域并转到不同的网站并返回(即从 http://localhost:8080 到 http://google.com 然后单击返回按钮去返回 http://localhost:8080):
window.addEventListener( "pageshow", function ( event ) {
var historyTraversal = event.persisted ||
( typeof window.performance != "undefined" &&
window.performance.navigation.type === 2 );
if ( historyTraversal ) {
window.location.reload();
document.getElementById('topic').value = "";
}
});
但是当我从 http://localhost:8080 转到 http://localhost:8080/differentpage 然后单击返回按钮返回 http://localhost:8080 它不会重新加载页面。我怎样才能让这个动作重新加载页面?
【问题讨论】:
-
尽量不要使用后退按钮。它是浏览器的功能,未来不同的浏览器可能会以不同的方式实现它。此外,用户通常希望后退按钮真正将他们带回来,改变这会恶化用户体验。
标签: javascript html