Javascript 在帧中设置location.hash

Javascript 在帧中设置location.hash,javascript,ajax,google-chrome,frameset,fragment-identifier,Javascript,Ajax,Google Chrome,Frameset,Fragment Identifier,我正在使用ajax更新页面在框架中的位置。但是当设置散列的位置时(在Chrome和一些IE(5.5)版本上,但偶尔在IE7上),页面被重新加载 下面的html演示了这个问题 主框架。。。。frame.html是 <html><head> <frameset rows="*"> <frame src=sethash.html frameborder=0 scrolling=auto name=somebody> </frameset> &

我正在使用ajax更新页面在框架中的位置。但是当设置散列的位置时(在Chrome和一些IE(5.5)版本上,但偶尔在IE7上),页面被重新加载

下面的html演示了这个问题

主框架。。。。frame.html是

<html><head>
<frameset rows="*">
<frame src=sethash.html frameborder=0 scrolling=auto name=somebody>
</frameset>
</head></html>

sethash.html页面为

<html><head>
<script language=JavaScript>
var Count = 0;
function sethash()
{  
  top.document.location.hash = "hash" + Count;  
  Count++;
}
</script>
</head>
<body onload="alert('loaded')">
<h1>Hello</h1>
<input type='button' onClick='sethash()' value='Set Hash'>
</body>
</html>`

var计数=0;
函数sethash()
{  
top.document.location.hash=“hash”+计数;
计数++;
}
你好
`
在大多数浏览器上,加载frame.html会在加载页面时显示一次加载的警报。然后,当按下set hash(设置哈希)按钮时,url将被更改,但加载的警报的哈希值将不再显示。在chrome和一些版本的IE上

Microsoft报告Internet Explorer 5.5可能存在相同的问题

我不能使用microsoft建议的解决方案,即捕获事件而不是触发它,而是滚动到视图中,因为我使用set-top.location.hash作为onLoad事件的一部分。

Webkit(扩展为Chrome)与location.hash的行为异常。关于它有一些公开的bug,最相关的可能是这个:它记录了当location.hash更改时页面刷新的问题。看起来你现在最好的选择就是交叉手指,希望它能尽快得到修复


不过,我无法在IE7中重现该漏洞,而您是我见过的第一个支持IE5.5的人,因此我无法在这方面真正帮助您;)

您是否尝试过创建指向散列的隐藏链接,并在其上均匀单击?

如何执行
location.href='#yourhash'也许它绕过了这个bug。

我也遇到了这个问题。我的情况允许我创建一个窗口取消绑定事件,将哈希值设置为我想要的值,以便用户浏览器进入下一页或刷新。这对我有用,但不确定对你有用

使用jquery,我做到了:

if($.browser.webkit){ $(window).unload(function() { top.window.location.hash = hash_value; }); }else{ top.window.location.hash = hash_value; } 如果($.browser.webkit){ $(窗口)。卸载(函数(){ top.window.location.hash=散列值; }); }否则{ top.window.location.hash=散列值; } 从技术上讲,您不需要进行webkit检查,但我认为对于使用firefox的人来说,在刷新框架集之前查看正确的哈希值可能会更好


-Jacob

对于chrome和safari,您需要使用window.location.href而不是window.location.hash来让哈希正常工作

请参阅下面的代码

function loadHash(varHash)
{
  if(varHash != undefined) {
    var strBrowser = navigator.userAgent.toLowerCase();

    if (strBrowser.indexOf('chrome') > 0 || strBrowser.indexOf('safari') > 0) {
        this.location.href = "#" + varHash;
    }
    else {
        this.window.location.hash = "#" + varHash;
    }
  }
}

我有一个变通方法,它涉及一个带有嵌入式iFrame的整页表。我用一张桌子替换了我的框架集,桌子占据了100%的高度和100%的宽度,有两行。然后,在每个表格单元格中,我放置了一个iframe,它占据了每个单元格100%的高度和宽度。从本质上讲,它模仿的是框架集,而不是框架集。最重要的是,哈希更改时无需重新加载

这是我的旧代码:

<html>
<head>
<title>Title</title>
</head>
<frameset rows="48,*" frameborder="yes" border="1">
    <frame name="header" noresize="noresize" scrolling="no" src="http://header" target="_top">
    <frame name="main" src="http://body" target="_top">
    <noframes>
    <body>
    <p>This page uses frames, but your browser doesn&#39;t support them.</p>
    </body>
    </noframes>
</frameset>
</html>

标题
此页面使用框架,但您的浏览器不使用';我不支持他们

…这是我的新代码:

<html>
<head>
<title>Title</title>
<style type="text/css">
body { margin: 0px; padding: 0px; border: none; height: 100%; }
table { height:100%; width:100% }
td { padding: 0px; margin: 0px; border: none; }
td.header { height: 48px; background-color: black; }
td.body { background-color: silver; }
iframe.header { border: none; width: 100%; height: 100%; }
iframe.body { border: none; width: 100%; height: 100%; }
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0">
  <tr><td class="header"><iframe class="header" src="http://header" target="_top" scrolling="no"></iframe></td></tr>
  <tr><td class="body"><iframe class="body" src="http://body" scrolling="no"></iframe></td></tr>
</table>
</body>
</html>

标题
正文{边距:0px;填充:0px;边框:无;高度:100%;}
表{高度:100%;宽度:100%}
td{填充:0px;边距:0px;边框:无;}
td.header{高度:48px;背景色:黑色;}
td.body{背景色:银色;}
iframe.header{边框:无;宽度:100%;高度:100%;}
iframe.body{边框:无;宽度:100%;高度:100%;}
希望这有帮助。

您也可以使用HTML5更改哈希,而无需在Chrome中重新加载页面。 大概是这样的:

// frameset hash change function
function changeHash(win, hash) {
    if(history.pushState) {
        win.history.replaceState(null, win.document.title, '#'+hash);
    } else {
        win.location.hash = hash;
    }
}
在第一个参数中,您需要传递frameset-top窗口。

这是有效的。小纸条:,看起来有点难看。