In Javascript, let\'s say we have a main page (main.html) which contains an (iframe.html)
Now inside this ifr
iframes (and frames) are their own windows, even though in the case of iframes they look like they're part of the main document's window. So yes, to refer to the main document's window, they'd use parent (or window.parent if you want to be verbose, but clear), because they are separate objects. This is partially necessary because a lot of the things in document end up as properties on the containing window.
If you think about it, it makes sense: The purpose of an iframe is to embed independently-sourced content within the page. If the main page and the iframe(s) on it shared a single window object, they'd be sharing global context, and quite possibly conflicting with one another.
Gratuitous live example:
Parent's HTML:
I'm the parent window
Parent's JavaScript:
function foo() {
display("foo called!");
}
function display(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
Child's HTML:
I'm in the frame
Child's JavaScript:
window.onload = function() {
document.getElementById('theButton').onclick = function() {
var p = window.parent;
if (!p) {
display("Can't find parent window");
}
else if (typeof p.foo !== "function") {
display("Found parent window, but can't find foo function on it");
}
else {
display("Calling parent window's foo function.");
p.foo();
}
};
function display(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
};