Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

javascript - Access child iFrame DOM from parent page

Here is the deal:

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

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');}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...