我正在尝试在 javascript 中修改页面的 URL(实际上是父框架集),如下所示:
function b(str) {
window.top.location.hash = str // originally was "#"+str
return true
}
这是从 gen.php 中的锚点调用的,如下所示:
<a onclick="b('Page29.html')" href="Page29.html" target="content">
顶级页面如下所示:
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<frameset cols="320,*" frameborder="0" border="0" framespacing="0">
<frame name="menu" src="gen.php">
<frame name="content" src="about:blank">
</frameset>
</html>
这在 Firefox 中运行良好,但在 Safari 中(更新:AND)Chrome 只需更改锚点就会重新加载整个页面!这是一个问题,因为我的页面中有状态正在丢失。我怎样才能抑制这种行为?
这是我根据以下建议尝试的更新代码:
function b(str) {
top.location.href = "#"+str
return true
}
在这种情况下,当我单击链接时,顶层框架会下降,并且页面会替换为 gen.php#Page29.html。
更新:看起来 location.hash 在框架中不起作用是 WebKit 中的一个错误,自 2009 年以来出现: https ://bugs.webkit.org/show_bug.cgi?id=24578
这是我根据以下答案提出的解决方法:
function b(str) {
if(history.pushState) {
top.history.replaceState(null, top.document.title, '#'+str);
} else {
top.location.hash = hash;
}
}