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

时间:2012-08-08 13:50:40

标签: javascript html google-chrome safari frameset

我有以下页面:

<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的页面时,哈希被加载到框架[principal]中 但是,当我清除Hash(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();
});

1 个答案:

答案 0 :(得分:0)

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

但是使用POST可能比哈希更能解决你的问题。