Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:51:39+00:00 2026-05-30T09:51:39+00:00

I’m trying to write a function that will dump a recursive tree of window

  • 0

I’m trying to write a function that will dump a recursive tree of window for all browsers. A problem that I immediately realized I was going to have, had to do with infinite objects (window.window.window.window). Just for laughs, I tried it anyways, and I got an error as I expected. Uncaught RangeError: Maximum call stack size exceeded (testing in Chrome)

So the first approach to check against objects that were going to cause this was simply:

if (variable != 'window' && variable != 'top' && variable != 'self' && variable != 'frames')

I’m thinking maybe that would have worked, and I simply missed a couple. It was a good theory, but I still get the maximum stack error. So I decided to type window in Chrome’s console, and manually look for all of the [DOMWindow] types, to add to this list. While doing that, I noticed the Infinity: Infinity value, which brought me to my next approach:

if (typeof namespace[variable]['Infinity'] === 'undefined')

I still got the maximum stack error with that, so I did a bit of Google searching, and learned about isFinite, so now I have: (edit: actually I just realized isFinite isn’t what I thought it was)

if (isFinite(tree[variable]))

The error finally went away, but the problem with this approach is that all objects in window are returning false for this, so the recursion fails. I realize that some of the approaches probably aren’t even cross-browser compatible, but it would be nice if I could get it to at least work in one browser in the mean time.

So how can I check for objects that are going to cause an infinite loop?

Here’s my code, just for anyone who might be interested:

(function () {
    window.onload = function () {
        window.onload = ''; // don't want to get our own code
        console.log((function (namespace) {
            tree = {};
            for (var variable in namespace) {
                /* gonna need these later
                var variable_typeof = typeof namespace[variable],
                    variable_object_tostring = Object.prototype.toString(namespace[variable]);
                */

                //if (variable != 'window' && variable != 'top' && variable != 'self' && variable != 'frames')
                //if (typeof namespace[variable]['Infinity'] === 'undefined')
                if (isFinite(tree[variable]))
                    tree[variable] = arguments.callee(namespace[variable]);
                else tree[variable] = 'Infinity';
            }
            return tree;
        })(window)); // Start from root
    }
})();

Update:
Here is a working product of what I finally came up with, for anyone interested.
GGG is worthy of mention for his help.

function loop (namespace) {
    if (namespace['__infinite_test']) return '[[recursion]]'; // It's infinite
    namespace['__infinite_test'] = true; // Note that we've been through this object
    var tree = {};
    for (var variable in namespace) {
        try { // For an issue in Chrome throwing an error
            namespace[variable]['__tester'] = null;
            delete namespace[variable]['__tester'];
        }
        catch (e) {
            tree[variable] = namespace[variable];
            continue;
        }
        if (namespace.propertyIsEnumerable(variable)) tree[variable] = loop(namespace[variable]);
        else tree[variable] = namespace[variable];
    }
    return tree;
}
console.log(loop(window));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:51:40+00:00Added an answer on May 30, 2026 at 9:51 am

    One way to prevent infinite recursion in your problem is to keep track of a list of all objects you have already visited and if you encounter an object you’ve already visited, you don’t recurse into it.

    When you encounter an object that is not in your list, you add it to your list and then recurse into it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.