从父页面访问子iFrame DOM [英] Access child iFrame DOM from parent page

查看:95
本文介绍了从父页面访问子iFrame DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是此交易:

domain.com/page - 父页面(document.domain = domain.com)包含iframe
子。 domain.com/page - 子iframe(document.domain =未设置)位于子域

domain.com/page -- Parent page (document.domain=domain.com) contains an iframe sub.domain.com/page -- Child iframe (document.domain=not set) is on a subdomain

有没有办法访问该iframe的DOM或我是运气不好?

Is there any way to access the DOM of that iframe or am I out of luck?

同一个原始政策会阻止我在父页面中包含的iframe上强制使用document.domain吗?我认为这会破坏相同原始策略的目的......如果是这种情况,是否有任何解决方法来访问呈现的父页面上的iframe的DOM?

Does same origin policy block me from forcing a document.domain on an iframe contained within a parent page? I suppose that would defeat the purpose of the same origin policy... If that is the case, is there any workaround to access the DOM of the iframe on the rendered parent page?

推荐答案

有办法。加载iframe中的页面时,请执行以下操作

There is a way. When the page in the iframe loads, have it do the following

parent.childGetElementById = function (id) {return document.getElementById(id);}
parent.childLoaded();

这将在父页面的全局范围内创建一个函数(包含iframe)。然后在父母中,只需拥有以下内容

This will make a function in the global scope of the parent page (that contains the iframe). Then in the parent, just have the following

function childLoaded() {var dom = childGetElementById('someid');}

这是因为您可以控制加载到iframe中的页面...如果不这样做,你运气不好。

This is along as you have control of the page your loading into the iframe... if you do not, you are out of luck.

这篇关于从父页面访问子iFrame DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆