繁体   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