从父页面访问子iFrame DOM

tim*_*123 17 javascript iframe same-origin-policy

这是交易:

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

有没有办法访问那个iframe的DOM或者我运气不好?

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

Zoi*_*erg 28

有一种方法.当iframe中的页面加载时,请执行以下操作

parent.childGetElementById = function (id) {return document.getElementById(id);}
parent.childLoaded();
Run Code Online (Sandbox Code Playgroud)

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

function childLoaded() {var dom = childGetElementById('someid');}
Run Code Online (Sandbox Code Playgroud)

这是因为你可以控制加载到iframe中的页面...如果你没有,那你就不走运了.

  • 我无权访问 iframe 中加载的页面,但这回答了我的问题。谢谢! (2认同)

Gat*_*ats 8

这是一种浏览器安全措施,否则每个人都会在您登录时包装您的银行网站并浏览密码.

您可以从iframe与父级对话,但不能再次返回iframe.