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

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

问题描述

这是交易:

domain.com/page -- 父页面 (document.domain=domain.com) 包含一个 iframesub.domain.com/page -- 子 iframe(document.domain=not set)在子域上

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天全站免登陆