我有一个 iframe,我无法控制其内容。在 iframe 内部会有重定向,而不是在顶部窗口中重定向(这是我所期望的),重定向总是发生在 iFrame 中。我在这里尝试了解决方案:How to get redirect url before onload happens if no control on the redirection?但结果并不理想。
现在在 iframe 页面内,我想使用 $(window).unload()
并手动更改 top.window.location.href
以强制在顶部重定向 window 。那么有谁知道如何在点击之后和重定向页面的onload
之前检测要重定向到的地址?谢谢!
PS:重定向的页面是同一个域的。
最佳答案
我希望这可以帮助你,它只适用于相同域的 iframe 内容,新的属性沙箱将扩展可能性,但目前仅由 Chrome 实现。
1)检测IF何时加载
myIF.addEventListener("load",ifOnLoad,true);
2)获取对 IF Window 的引用,并在每次加载 iframe 时为 onBeforeUnload 事件注册一个监听器。
ifContentWindow = myIF.contentWindow;
ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);
3)当您调用处理程序(ifOnUnload)时,要尽早获取ifContentWindow.location,然后设置延迟来读取它。
setTimeout(delayedIFUnload, 100);
4)在delayedIFUnload函数中获取if位置并重定向您的主页。
window.location = ifContentWindow.location;
我只在我的环境(MAC、Chrome)中测试代码,您需要做一些工作来调整其他浏览器的代码。例如使用 addEventListener 或 contentWindow。
这是工作代码 http://pannonicaquartet.com/test/testif.html 的链接,我尝试在 fiddle 中执行此操作,但 fiddle 有很多框架,并且其中的代码无法正常工作。我对消息使用跨度,因为在这种类型的操作中,警报会被阻止,至少在 Chrome 中是这样。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var myIF, spNotify, ifContentWindow;
function init(){
myIF = document.getElementById("myIF");
spNotify = document.getElementById("spNotify");
myIF.src="testIF_1.html";
myIF.addEventListener("load",ifOnLoad,true);
}
function ifOnLoad(){
try{
ifContentWindow = myIF.contentWindow;
ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);
}catch(er){
alert(er);
}
}
function ifOnUnload(){
try{
notify(ifContentWindow.location);
setTimeout(delayedIFUnload, 100);
}catch(er){
alert(er);
}
}
function delayedIFUnload(){
try{
notify(ifContentWindow.location);
}catch(er){
alert(er);
}
}
function notify(what){
spNotify.innerText = what;
}
</script>
</head>
<body onload="init();">
<div id="dvMsg">Target: <span id = "spNotify"></span></div>
<iframe id="myIF" src=" " style="width:1100px;height:700px;" />
</body>
新年快乐!!!
<小时/><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var myIF, spNotify, ifContentWindow, lastURL, timerRef;
function init(){
myIF = document.getElementById("myIF");
spNotify = document.getElementById("spNotify");
myIF.src="testIF_1.html";
myIF.addEventListener("load",ifOnLoad,true);
}
function ifOnLoad(){
try{
ifContentWindow = myIF.contentWindow;
ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);
}catch(er){
alert(er);
}
}
function ifOnUnload(){
try{
notify(ifContentWindow.location);
lastURL = ifContentWindow.location;
timerRef = setInterval(delayedIFUnload, 5);
}catch(er){
alert(er);
}
}
function delayedIFUnload(){
try{
if(lastURL != ifContentWindow.location){
notify(ifContentWindow.location);
clearInterval(timerRef);
}
}catch(er){
alert(er);
}
}
function notify(what){
spNotify.innerText = what;
}
</script>
</head>
关于javascript - 有没有办法在加载之前检测 iframe 将重定向到的 src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14103618/