簡體   English   中英

單擊來自同一域的后退按鈕時重新加載網頁

[英]Reload webpage when clicking back button from same domain

對於網站,當用戶單擊按鈕時,會將他們帶到 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 時,它不會重新加載頁面。 我怎樣才能讓這個動作重新加載頁面?

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

我希望這會有所幫助,如果我真的有你的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM