Search code examples
javascriptinternet-explorerrecursionstack-overflow

Stack Overflow in IE recursion -> window.frames not equal to itself


I'm doing a recursion through the whole tree of javascript objects available from the window global scope.

Its all fine and dandy in Chrome, Firefox and Safari but I'm having a bit of a problem in Internet Explorer.

Basically I'm building a stack of objects as I traverse through the tree, so I can check down the line of an object has been asigned in more than one place. For example I check.

var ignoreStack = [window, document]; // grows over time to contain every object

ignoreStack.contains = function(obj) {
  for(var i=0;i<this.length;i++) {
    if (this[i] === obj) {return true;} 
  }
  return false;
};

// ... further down the line inside a recursion
if (!(obj instanceof Object)) {return;}
for (var prop in obj) { 
  if (ignoreStack.contains(obj[prop])) {return;}
  ignoreStack.push(obj[prop]);
  recurse(obj[prop], prop);
}

Now the only problem is when it checks the following:

// Returns true in Chrome, Safari and Firefox
console.log(window.frames.frames === window.frames);
// Return false in IE

Every other object in the tree is equal to itself, however window.frames in IE seems to be the only exception where its not true to itself, so the recursion continues on as follows:

// window.frames.frames.frames.frames.frames ...etc

Does anyone have any idea why window.frames is not equal to itself?

I've tried doing some digging in google and debugging using IE Developer tools, all I can gather is that its an object of type DispHTMLWindow2 (not that that means much to me) and that (unlike in other browsers) its neither an Object nor an Array.

// Returns true in Chrome, Safari and Firefox
console.log(window.frames instanceof Object);
// Return false in IE

If somebody could shed some light on the reason why window.frames in IE behaves unlike any other javascript object (causing this type of infinite recursion) that would be much appreciated.

Thanks in advance.


Solution

  • Related question about other host objects that are not equal in IE

    If somebody could shed some light on the reason why window.frames in IE behaves unlike any other javascript object (causing this type of infinite recursion) that would be much appreciated.

    window.frames is a host object. Host objects are allowed to do whatever they please in ES3. IE is notorious for having highly misbehaving host objects. There is little you can do about it.

    I recommend you do the dirty hack which is a hard coded check for whether the key is 'frames' and the obj is window and ignoring the entire thing.

    There are another bunch of properties inside window.frames that also don't behave.