【问题标题】:window.location.hash = ""; forces frameset reload on Chrome and Safariwindow.location.hash = "";强制在 Chrome 和 Safari 上重新加载框架集
【发布时间】:2012-08-05 15:36:43
【问题描述】:

我有以下页面:

<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();
});

【问题讨论】:

  • 试着把问题缩短到重点。
  • @ColBeseder 感谢您的提示。已经编辑了我的问题:)

标签: javascript html google-chrome safari frameset


【解决方案1】:

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

但也许使用 POST 比散列更能解决您的问题。

【讨论】:

  • 你说对了一半!我用添加和删除哈希的按钮在 chrome 上做了一个简单的测试,它不会重新加载。这让我对自己的问题更加绝望,让我找到了原因!在框架集上,它会重新加载! link
猜你喜欢
  • 2012-01-10
  • 1970-01-01
  • 2013-12-05
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多