Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting entire window object to string

Tags:

javascript

I need to examine the entire window object, in order to check where data is actually being saved.

If i execute window.toString() i get "[object Window]" I have also tried with the JSON.stringify(window) function and got the following error:

VM18766:1 Uncaught TypeError: Converting circular structure to JSON

Is there any way i can get the entire javascript object content including prototype functions?

I need this in order so that i can search inside the object text for specific content being saved in an object and where this object is.

like image 415
Aryeh Armon Avatar asked Jun 08 '16 08:06

Aryeh Armon


People also ask

How do you transform an object into a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Which of the function is used to convert object to string?

The answer is option D (str(x)). str(x) converts the object to a string in python.

Which method is useful to get all values of object in a string form?

values() Method: The Object. values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object.

How do you convert JavaScript object to string explain with example?

Example 1: Convert Object to String Using JSON. stringify() method is used to convert an object to a string. The typeof operator gives the data type of the result variable.


1 Answers

if you try to do JSON.stringify(window), you will get the following error:

Uncaught TypeError: Converting circular structure to JSON

From this this StackOverflow post, you can do the following:

const p = document.getElementById('p');

const getCircularReplacer = () => {
    const seen = new WeakSet();
    return (key, value) => {
        if (typeof value === "object" && value !== null) {
            if (seen.has(value)) {
                return;
            }
            seen.add(value);
        }
        return value;
    };
};

p.innerHTML = JSON.stringify(window, getCircularReplacer());
<html>
<head>
</head>
<body>
<p id="p"></p>
<script src="app.js"></script>
</body>
</html>
like image 59
Anthony Avatar answered Oct 05 '22 19:10

Anthony