List all js global variables used by site (not all defined!)

前端 未结 9 1891
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 13:16

What is the way to list all global variables that have been used by the site? Can any browser javascript debugger do that? By used I mean READ, not changed/added. Detect ifr

9条回答
  •  囚心锁ツ
    2020-12-04 13:24

    Since this question is the first in google when searching for a way how to list global javascript variables, I will add my own answer for that. Sometimes you need to list global variables to see if your code does not have a variable leaked outside the scope (defined without 'var'). For that, use this in the debug console:

    (function ()
    {
       var keys=Object.keys( window );
       for (var i in keys)
       {
          if (typeof window[keys[i]] != 'function')
          console.log(keys[i], window[keys[i]]);
       }
    })();
    

    It will list the standard global variables, like window, document, location, etc. Those are just few. So you can find your leaked vars in the list easily.

提交回复
热议问题