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:

(function(){
  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 : http://johnkpaul.com/blog/2013/07/20/break-on-property-change/

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s