eva*_*ong 5 html javascript iframe html5
我有一个包含iframe的页面,我只想跟踪iframe的历史记录.我试着像这样使用历史对象:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function checkFrameHistory() {
alert(window.frames["test2"].history.length);
}
function checkThisHistory() {
alert(history.length);
}
</script>
</head>
<body>
<iframe name="test2" src="test2.html"></iframe>
<div>
<input type="button" onclick="checkFrameHistory()" value="Frame history"/>
<input type="button" onclick="checkThisHistory()" value="This history"/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
test2.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function checkMyHistory() {
alert(history.length);
}
</script>
</head>
<body>
<div>
Test2
</div>
<div>
<input type="button" onclick="checkMyHistory()" value="My history"/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但它实际上给了我包括父窗口的历史记录.
例如,我去了主窗口的另一个站点,然后回到我的iframe测试站点.双方window.frames["test2"].history.length并history.length仍然由2增加长度给了我同样的计数.
我做错了吗?有没有办法只跟踪iframe历史记录?
Chrome在窗口基础上管理历史记录;不在单独的框架上。此行为可能是您的问题的原因。
不知道它是否是所有浏览器的默认行为。
编辑:您必须维护您的Navigationhistory服务器端。如另一个答案中所述,在主窗口上保留数组不会在您还使用父窗口进行导航时起作用。
由于同源策略,您不太可能访问与框架关联的任何对象。
您是否尝试过将页面本身和 iframe 上的域设置为相等?例如
//somewhere on your page in a script tag
document.domain = "your-top-level-domain.com";
Run Code Online (Sandbox Code Playgroud)
这应该向浏览器发出信号,表明页面相互信任,并且应该有权访问其内部状态