window.location.hash = “”;强制在Chrome和Safari上重新加载框架集

window.location.hash = ""; forces frameset reload on Chrome and Safari

本文关键字:新加载 Safari 框架 加载 Chrome hash location window      更新时间:2023-09-26

我有以下页面:

<head>
<script type="text/javascript">
  $(document).ready(function(){
    var hash = window.location.hash;
    var src = hash.substring(1); //remove #
    window.location.hash = "";
    if(src != ''){
      frames['principal'].document.location.href = decodeURIComponent(src);
    }
  });  
</script>
</head>
<frameset rows="18%,*" class="pagecontainer">
  <frame name="top" noresize src="site/paginatop.htm" target="top" scrolling="no" frameborder="0"/>
  <frameset cols="11%,*">
    <frame name="lateral" noresize src="site/paginalateral.htm" target="lateral" frameborder="0"/>
    <frame name="principal" id="principal" noresize src="site/paginahome.htm" target="principal" frameborder="0"/>
  </frameset>
</frameset>
当我在哈希中加载

带有URL的页面时,哈希值将加载到框架[主体]中。
但是,当我清除哈希(window.location.hash = ";)时,Chrome 和 Safari 会重新加载页面,因此 frame[principal] 获得默认值 (site/paginahome.htm)。我已经发现了一个报告该行为的错误 https://bugs.webkit.org/show_bug.cgi?id=24578
任何解决方法将不胜感激!

提前谢谢。

解决
我找到了解决window.history.replaceState问题的方法:

$(document).ready(function(){
  var src = getHash();
  if(src != ''){
    frames['principal'].document.location.href = decodeURIComponent(src);
    var strBrowser = navigator.userAgent.toLowerCase();
    if (strBrowser.indexOf('chrome') > 0 || strBrowser.indexOf('safari') > 0) {
      if(history.pushState) {
        window.history.replaceState(null, window.document.title, '#');
      }
    }
    else {
      window.location.hash = "";
    }
  }
  setFavicon();
});

在 Chrome 中,window.location.hash = "" 不会重新加载页面。它只是将其滚动到顶部顶部。

但也许使用 POST 会比哈希更好地解决您的问题。