Detecting inside which iframe a script is executed
I have a page with multiple iframes. One of these iframes has a page from a different domain. Inside this iframe is another iframe with a page from the parent domain.
my page from mydomain.com
-> an iframe
-> iframe "#foo" from another-domain.com>
-> iframe "#bar" from mydomain.com
-> another iframe
I need to get a link to the "#foo" node on the main page. The security model should allow me to do this because "#bar" has the same domain as the main page. So what I'm doing is iterating through the array window.top
and comparing each item to the object window
that is currently the "#bar" "window" object. My test code looks like this:
for (var i = 0; i < top.length; i++) {
for (var j = 0; j < top[i].length; j++) {
if (top[i][j] == window) {
alert("The iframe number " + i + " contains me");
}
}
}
This works fine in all browsers, but Internet Explorer 6 throws a security error on access top[i][j]
. Any ideas on how to fix this issue on IE6?
Thanks!
a source to share
The employee found a solution: a tree stood up instead.
var getLastParent = function (baseWindow, topWindow) {
var lastParent, nextParent;
lastParent = nextParent = baseWindow;
while (nextParent != topWindow && nextParent != nextParent.parent) {
lastParent = nextParent;
nextParent = nextParent.parent;
}
return lastParent;
};
var findWindow = function (baseWindow, topWindow) {
var lastParent = getLastParent(baseWindow, topWindow);
for (var i = 0; i < topWindow.length; i++) {
if (topWindow[i] == lastParent)
return i;
}
return -1;
};
a source to share
It looks like IE is unhappy with accessing any properties try[i]
as it is part of a different security context, even if your resource is accessed in the same security context as the script.
You may be out of luck. However, you tried to replace:
- try [i]
- try [i] [j]
from:
- try.frames [i]
- try.frames [I] .frames [J]
It should be more or less the same. This is a long shot and I don't know if it will work, but it might just be.
a source to share