Find what is setting a global variable in javascript

Having identified an unknown global variable in your JavaScript application, how should you go about finding what is setting it?

Well one way is to watch for the variable being set as follows.  In this example, I look for something setting the ‘value’ global variable:

  var value;
  Object.defineProperty(window, 'value', {
    get: function(){ return value; },
    set: function(val) {
      value = val;
      debugger; // BOOM!

Just paste that into your debugger console, and run your code.  To find other global variables, just change ‘value’ to the name of the variable you are interested in.

Credit to :

About austinfrance

Technical Developer @ RedSky IT / Explorer Software
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s