2016-11-20 87 views
0

我已的iframe页面:更改父框架URL的iframe网址变更

<iframe src="http://externalwebsite.com/test.html" scrolling="no" height="400px" width="400px" allowfullscreen></iframe> 

而且我想,如果iframe网址更改http://externalwebsite.com/succes.html

我试图找出父URL更改为http://mywebsite.com/succes.html它用JavaScript,但我没有设法做到这一点。有人能帮我吗?

+1

由于安全问题有可能没有办法做到这一点没有进行代理的外部网站 – mplungjan

+0

当它加载了新的一页,但不是网页是什么的时候不相同的域http://plnkr.co/edit/您可以检测ebnk244eJrwREIJuI3JC?p =预览iframe由于*“相同来源策略”而非常受限制* – charlietfl

回答

0

<iframe id="frameId" src="http://externalwebsite.com/test.html" scrolling="no" height="400px" width="400px" allowfullscreen></iframe>

绑定到父窗口frame.onload事件。

var frame = document.getElementById('frameId'); 
frame.onload = function(e){ 
    if(frame.src === 'http://externalwebsite.com/succes.html'){ 
     window.location.href = 'http://externalwebsite.com/succes.html'; 
    } 
}; 

尝试frame.src='someUrl';进行测试。

+0

您测试过吗?上面的演示链接不会给更新网址,而且jQuery也没有'onload',所以它不会像写入一样激发。或者是'frame [0] .onload'或者frame.on('load'...' – charlietfl

+0

:(对于我来说它没有奏效,但是谢谢你试图帮助 – jollesnaas

+0

你是对的charlietfl。你提到的代码或 var frame = document.getElementById('frameId'); – napoleonss