【问题标题】:Reload webpage when clicking back button from same domain单击同一域的后退按钮时重新加载网页
【发布时间】: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


【解决方案1】:

使用window.location.href; 像这样:

<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);

如果我真的有你的问题,我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多