alternative to html top.document inside iframe
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 ad开发者_开发问答vance!
Rui
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.
精彩评论