簡體   English   中英

如何通過history.pushState()更改URL時,如何使用原始URL刷新頁面

[英]how to refresh the page with original URL when URL has been changed via history.pushState()

我使用了history.pushState(),現在如果用戶刷新頁面,那么它是刷新的當前URL,它不是原始URL。

我嘗試用cookie檢測刷新頁面,隱藏歸檔,但它無法正常工作。

window.onload = function() {
document.cookie="PR=0";     

var read_cookies = document.cookie;


var s = read_cookies;
s = s.substring(0, s.indexOf(';'));

  if( s.includes("1"))
{
    window.location.href = "https://www.google.com";


}
else{

    document.cookie="PR=1";     
}

  loadURL();

};


function loadURL()
  {
  document.cookie="PR=1";       

  document.getElementById("visited").value="1";


  var str="abc/b cd";
  str=str.replace(/ /g, "-");
  history.pushState({},"",str);
  }

當用戶刷新頁面時,我需要在那個時間使用原始URL。

這可能會有所幫助。 但是你需要控制推送的網址。

// this goes to your 'original' url
window.addEventListener('beforeunload', function (event) {
    sessionStorage.setItem('lastPage', window.location.href)
}

// page with your pushed url
if (sessionStorage.getItem('lastPage') === 'PAGE_YOU_DONT_WANT_TO_BE_REACHABLE_DIRECTLY') {
    window.location = 'PAGE_YOU_WANT'
}

我很感興趣的用例是什么。 據我所知,你無法完全抑制刷新事件。

暫無
暫無

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

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