javascript - alternative to html top.document inside iframe - Stack Overflow

We have a web application where we're using "top.document" since this application uses i

We have a web application where we're using "top.document" since this application uses iframes and sometimes we must reach the top document.

The problem started now since we need to integrate this application in a website and we put it inside an iFrame in this very site. Since the application uses "top.document" it is reaching the site's "document" instead of the application's "top".

Do you have any ideas to help us solve the problem?

Thanks in advance!

Rui

We have a web application where we're using "top.document" since this application uses iframes and sometimes we must reach the top document.

The problem started now since we need to integrate this application in a website and we put it inside an iFrame in this very site. Since the application uses "top.document" it is reaching the site's "document" instead of the application's "top".

Do you have any ideas to help us solve the problem?

Thanks in advance!

Rui

Share Improve this question asked Nov 5, 2009 at 16:10 RuiRui 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

Assuming your iframe is one level deep:

parent.document

or, if it's deeper:

parent.parent.parent.parent.document

but I really hope it's not that deep ;-)

The approach to ensuring that you are looking at the correct window is to know where you are within the framed application.

For example, let us say the “main frame” is the root of the site. Within the main frame, additional ‘iFrames’ are defined. The pages that are loaded in these additional frames need to know where they are in the frame hierarchy.

So you see, understanding where you are in the hierarchy of frames and pop-ups will allow you to properly backtrack and define references to functions and objects contained within windows in previous pages/frames.

Adding some Javascript code to give you an idea:

if (window.self){
    if (window.top == window) {
        if (window.name == "main_frame"){
            MainFrameRef = window.top;
            return MainFrameRef;
         } else {
            return false;
         }
    }
}

If you are looking for dead simple, look for known ids, and that's where you stop your upward crawl of parents and tops and docs.

Or you could have each frame register itself in some global variable (namespaced of course) when they load, that way you have a reference to them and do not need to traverse the DOM.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745640708a4637673.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信