访问动态创建的跨域iFrame中的元素? [英] Access an element inside a dynamically-created, cross-domain iFrame?
问题描述
我想访问iFrame中的元素,但是因为iFrame被调用的方式,我不会成功。
I want to access an element inside an iFrame, but because of the way the iFrame is being called, I am not succeeding.
这是目标页面(www1.k9webprotection.com / ...)
iframe位于不同的子网域上:
The iframe is on a different subdomain:
<iframe src="http://license.k9webprotection.com/..." ...></iframe>
为iframe加载时设置超时或事件侦听器没有帮助。
Setting a timeout or an event listener for when the iframe is loaded, did not help.
推荐答案
这两个文档都位于不同的(子)域,默认情况下它们不能通过javascript进行交互。
Both documents are placed on different (sub)domains, by default they are not able to interact via javascript.
您必须将两个文档的 domain
设置为相同的值。
You must set the domain
of both documents to the same value.
将这个位置放在两个页面的< head />
中:
Put this somewhere in the <head/>
of both pages:
<script type="text/javascript">
document.domain='k9webprotection.com'
</script>
...然后等待 onload
- iframe的事件,您应该能够从父页面访问iframe中的文档(反之亦然)。
...then wait for the onload
-event of the iframe and you should be able to access the document inside the iframe from the parent page(and vice versa).
GreaseMonkey的示例脚本iframe的主体):
Sample-Script for GreaseMonkey(simply overwrites the body of the iframe):
// ==UserScript==
// @name k9
// @namespace k9
// @include http://www1.k9webprotection.com/get-k9-web-protection-free
// @include http://license.k9webprotection.com/license.jsp*
// @version 1
// @grant none
// ==/UserScript==
document.domain= 'k9webprotection.com';
if(self===top){
try{
$('iframe.getk9iframe').load(function(){
$('body',this.contentDocument)
.text('gotcha')
.css({background:'red',fontSize:'3em'});
});
alert("I'm the document in the top-window");
}
catch(e){}
}
这篇关于访问动态创建的跨域iFrame中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!