Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - Reading parent document from iFrame and changing parent

I'm trying to write a JavaScript application that will load a button up in an iFrame on a page. This application will read the parent document and strip out all images, apply minimal styling and reload the parent with this newly marked up page.

The problem I am having is reading and writing the parent document from the iFrame. Does anyone know of a way to achieve this? I've read up a little on cross-domain messaging but am unsure of the alternatives and which would be best.

Thanks in advance.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can reference the parent either by using parent or using the top variable that always points to the outer most document.

  • parent == parent window
  • top == out most window

So for accesing the first div in the parent window

var d = parent.document.getElementsByTagName("div")[0];

But as noted already in the comment, both the outer document and the document in the iframe need to be from the same domain or you will be blocked for security reasons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...