tags:

views:

26

answers:

2

I have a page with several iframes. One of this iframes has a page from a different domain. Inside this iframe there's 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 reference to the "#foo" node inside the main page. The security model should allow me to do that because "#bar" has the same domain as the main page. So what I'm doing is iterating through the window.top array and comparing each element to the window object which is currently the "#bar" window object. My test code looks like:

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 when accesing top[i][j]. Any ideas on how to solve this on IE6?

Thanks!

+1  A: 

It looks like IE is not happy with you access any properties of try[i] since it's part of a different security context, even though the property you're accessing would be under the same security context as the script.

You may be out of luck here. However, have you tried replacing:

  • try[i]
  • try[i][j]

with:

  • try.frames[i]
  • try.frames[i].frames[j]

This should be more or less the same. It's a long shot, and I don't know if it'll work, but it could just.

thomasrutter
Yup, I tried using the frames object but it had the same effect.
juandopazo
A: 

A coworker found the solution: going up the tree 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;
};
juandopazo