简体   繁体   English

单击来自同一域的后退按钮时重新加载网页

[英]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.

相关问题 是否可以在单击浏览器后退按钮时重新加载同一页面? - is it possible to reload the same page on clicking browsers back button? 通过在Firefox中单击“后退”按钮来重新加载访问时的页面 - Reload the page when visited by clicking Back button in Firefox 当从另一个域中单击“后退”按钮时,浏览器是否会重新加载页面? - Does a browser reload a page when `back` button is clicked from another domain 如何在单击后退按钮时阻止网页加载? - how to stop a webpage from loading on clicking back button? 通过单击后退按钮重新加载上一页 - Reload the previous page by clicking the back button 用户提交后退按钮时如何重新加载同一页面 - How to reload the same page when user submits the back button 单击浏览器的后退按钮时将客户返回到同一位置 - Return customer to same position when clicking browser's back button 单击浏览器后退按钮时如何保持在同一页面上 - How to stay on the same page when clicking the browser back button 如何通过单击“返回”按钮强制Web浏览器在查看文档时重新加载文档? - How can I force a web browser to reload the document when it is viewed by clicking the Back button? 用户单击上一页的后退按钮后如何重新加载网页? - How to reload a webpage after user clicks back button from previous page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM