View list of all JavaScript variables in Google Chrome Console

后端 未结 15 2149
星月不相逢
星月不相逢 2020-11-29 14:32

In Firebug, the DOM tab shows a list of all your public variables and objects. In Chrome\'s console you have to type the name of the public variable or object you want to ex

15条回答
  •  天涯浪人
    2020-11-29 14:58

    David Walsh has a nice solution for this. Here is my take on this, combining his solution with what has been discovered on this thread as well.

    https://davidwalsh.name/global-variables-javascript

    x = {};
    var iframe = document.createElement('iframe');
    iframe.onload = function() {
        var standardGlobals = Object.keys(iframe.contentWindow);
        for(var b in window) { 
          const prop = window[b];
          if(window.hasOwnProperty(b) && prop && !prop.toString().includes('native code') && !standardGlobals.includes(b)) {
            x[b] = prop;
          }
        }
        console.log(x)
    };
    iframe.src = 'about:blank';
    document.body.appendChild(iframe);
    

    x now has only the globals.

提交回复
热议问题