javascript 仅跟踪 iframe 历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10745564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Track only iframe history
提问by evanwong
I have a page which contains an iframe and I want to track the history of the iframe only. I tried to use the history object like this:
我有一个包含 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>
test2.html
测试2.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>
but it actually gave me the history including the parent window.
但它实际上给了我包括父窗口在内的历史。
For instance, I went to another site in the main window and came back to my iframe test site. Both window.frames["test2"].history.length
and history.length
still gave me the same count by increasing the length by 2.
例如,我去了主窗口中的另一个站点,然后又回到了我的 iframe 测试站点。双方window.frames["test2"].history.length
并history.length
通过2增加长度还是给了我同样的数量。
Did I do it wrong? Is there a way to track only the iframe history?
我做错了吗?有没有办法只跟踪 iframe 历史记录?
回答by Ruben-J
Chrome manages history on window base; not on separate frames. This behaviour is probably the cause of your problem.
Chrome 基于窗口管理历史记录;不在单独的框架上。这种行为可能是导致您出现问题的原因。
Dont know if its default behaviour for all browsers btw.
顺便说一句,不知道它是否适用于所有浏览器的默认行为。
Edit: You have to maintain your navigationhistory server side. Keeping an array on the mainwindow, as stated in another answer, doesnt work when you also navigate with the parent window.
编辑:您必须维护您的导航历史服务器端。如另一个答案中所述,在主窗口上保留数组在您还使用父窗口导航时不起作用。
回答by David Mulder
There is an easy way to do this, provided of course that the iframe content is the same domain.
有一种简单的方法可以做到这一点,当然前提是 iframe 内容是同一个域。
From the parent you bind to the beforeUnload
or the load
event of the iframe's window object. In the event's callback handler you simply do something along the lines of locations.push(getElementById("iframe").contentWindow.location.href)
where locations is of course a previously defined array in the parent scope.
从父级绑定到iframe 的 window 对象的beforeUnload
或load
事件。在事件的回调处理程序中,您只需按照locations.push(getElementById("iframe").contentWindow.location.href)
where 位置当然是父作用域中先前定义的数组来做一些事情。
回答by Raab
use this to access the history of iframe
使用它来访问 iframe 的历史记录
document.getElementById("test2").contentWindow.history.length
回答by WickyNilliams
It's unlikely you will be able to access any objects associated with the frame because of the same-origin policy.
由于同源策略,您不太可能访问与框架关联的任何对象。
Have you tried setting the domains to be equal on the page itself and the iframe? e.g.
您是否尝试将页面本身和 iframe 上的域设置为相等?例如
//somewhere on your page in a script tag
document.domain = "your-top-level-domain.com";
this should signal to the browser that the pages trust each other and should be given access to their internal state
这应该向浏览器发出信号,页面相互信任,并且应该被授予访问其内部状态的权限
回答by Chris Li
If the frame contains contentWindow, you can use frame.contentWindow.history.
如果框架包含contentWindow,则可以使用frame.contentWindow.history。
But not all browsers support this property. If not, the frame's location should be stored in some variables. Bind event handler on your frame onload event like below(using jquery)
但并非所有浏览器都支持此属性。如果没有,框架的位置应该存储在一些变量中。在您的框架 onload 事件上绑定事件处理程序,如下所示(使用 jquery)
var frameHistory = [];
$(document).ready(function(){
var frame = window.frames["test2"];
$.bind(frame, "onload", function(){ //store location when frame loads
frameHistory.push(frame.window.location.href);
}
});
And in your button click event handler:
在您的按钮单击事件处理程序中:
alert(frameHistory.length);
回答by Dan Kanze
ITS POSSIBLEto beat SOP as long as you have access to both domains. You can use a helper IFRAME to pass onchange events from the BODY.
只要您可以访问这两个域,就有可能击败 SOP。您可以使用帮助程序 IFRAME 从 BODY 传递 onchange 事件。
This post should be extremely helpful: Resizing an iframe based on content
这篇文章应该非常有用: Resizing an iframe based on content