[英]Reload webpage when clicking back button from same domain
For a website when a user clicks a button it takes them to http://localhost:8080/differentpage.对于网站,当用户单击按钮时,会将他们带到 http://localhost:8080/differentpage。 What I would like to happen is when they click the back button in their browser it reloads http://localhost:8080.
我想要发生的是当他们单击浏览器中的后退按钮时,它会重新加载 http://localhost:8080。
Using the below script I was able to get the desired result if I left the domain and went to a different website and back (ie. from http://localhost:8080 to http://google.com then click the back button to go back to 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 = "";
}
});
But when I go from http://localhost:8080 to http://localhost:8080/differentpage then click back button back to http://localhost:8080 it does not reload the page.但是当我从 http://localhost:8080 转到 http://localhost:8080/differentpage 然后单击返回按钮返回 http://localhost:8080 时,它不会重新加载页面。 How can I make it so this action reloads the page?
我怎样才能让这个动作重新加载页面?
Use window.location.href;
使用
window.location.href;
Like this:像这样:
<html>
<head>
<title>Back Button</title>
</head>
<body>
<button type="button" name="" id="back:>Back</button>
</body>
</html>
let backButton = document.querySelector('back');
function goBack(e) {
e.preventDefault();
window.location.href = 'http://localhost:8080';
}
backButton.addEventLister('click', goBack);
I hope this will help, if I have really got your question!我希望这会有所帮助,如果我真的有你的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.