283

在 Firebug 中,DOM 选项卡显示所有公共变量和对象的列表。在 Chrome 的控制台中,您必须键入要探索的公共变量或对象的名称。

Chrome 的控制台是否有办法(或至少是命令)显示所有公共变量和对象的列表?它将节省大量打字。

4

16 回答 16

379

这是您正在寻找的输出吗?

for(var b in window) { 
  if(window.hasOwnProperty(b)) console.log(b); 
}

这将列出window对象上可用的所有内容(所有函数和变量,例如,$以及jQuery在此页面上等)。不过,这是一个相当多的清单;不知道它有多大帮助......

否则就这样做window并开始沿着它的树走:

window

这将为您提供DOMWindow一个可扩展/可探索的对象。

于 2010-05-29T11:28:29.023 回答
81

当脚本执行停止时(例如,在断点处),您可以简单地在 Developer Tools 窗口的右窗格中查看所有全局变量:

铬全局

于 2010-05-29T12:50:58.030 回答
75

打开控制台,然后输入:

  • keys(window)查看变量
  • dir(window)看物体
于 2013-04-12T04:21:17.103 回答
44

window对象包含所有公共变量,因此您可以在控制台中键入它,然后展开以查看所有变量/属性/函数。

chrome-show-all-variables-expand-window-对象

于 2010-05-29T11:27:31.757 回答
42

如果您想排除窗口对象的所有标准属性并查看特定于应用程序的全局变量,这会将它们打印到 Chrome 控制台:

{

    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);

    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
}

该脚本可以很好地用作书签。要将脚本用作书签,请创建一个新书签并将 URL 替换为以下内容:

javascript:(() => {
    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);
    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
})()
于 2014-03-31T20:06:05.410 回答
12

在 javascript 控制台中键入以下语句:

debugger

现在您可以使用常规调试工具检查全局范围。

公平地说,您将获得范围内的所有内容window,包括内置浏览器,因此这可能是一种大海捞针的体验。:/

于 2018-09-27T13:58:33.987 回答
10

大卫沃尔什对此有一个很好的解决方案。这是我对此的看法,将他的解决方案与在这个线程上发现的内容结合起来。

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现在只有全局变量。

于 2016-08-29T19:47:41.430 回答
6

Avindra 提到的同一篇文章中的更新方法— 注入 iframe 并将其contentWindow属性与全局窗口属性进行比较。

(function() {
  var iframe = document.createElement('iframe');
  iframe.onload = function() {
    var iframeKeys = Object.keys(iframe.contentWindow);
    Object.keys(window).forEach(function(key) {
      if(!(iframeKeys.indexOf(key) > -1)) {
        console.log(key);
      }
    });
  };
  iframe.src = 'about:blank';
  document.body.appendChild(iframe);
})();

于 2016-11-04T06:59:03.553 回答
5

要查看 chrome 中的任何变量,请转到“Sources”,然后“Watch”并添加它。如果您在此处添加“窗口”变量,那么您可以展开它并进行探索。

于 2016-09-16T07:38:04.047 回答
4

你可能想试试这个适用于 Chrome 的Firebug lite扩展。

于 2010-05-29T12:23:02.637 回答
4

类型:this在控制台中,

要获得我认为的(?),我认为这与在控制台window object中输入基本相同。window

它至少在 Firefox 和 chrome 中有效。

于 2019-08-14T08:52:26.887 回答
3

列出变量及其值

for(var b in window) { if(window.hasOwnProperty(b)) console.log(b+" = "+window[b]); }

在此处输入图像描述

显示特定变量对象的值

console.log(JSON.stringify(content_of_some_variable_object))

在此处输入图像描述

资料来源:@northern-bradley 的评论和@nick-craver 的回答

于 2020-01-18T18:07:50.480 回答
3

我最终将其用于调试目的:

for (aProperty in window) {
    try{
        console.log(aProperty +':'+JSON.stringify(window[aProperty]));
    }catch{}
}

try用于避免TypeError: Converting circular structure to JSON
然后Save as...控制台输出到文件并进一步操作。

于 2021-05-19T18:12:22.557 回答
1

由于所有“公共变量”实际上都是窗口对象(您正在查看的窗口/选项卡)的属性,因此您可以只检查“窗口”对象。如果您有多个框架,则无论如何都必须选择正确的窗口对象(如在 Firebug 中)。

于 2010-05-29T11:28:03.873 回答
1

试试这个简单的命令:

控制台日志(窗口)
于 2019-01-18T18:35:25.363 回答
-1

在此处输入图像描述

于 2011-12-03T08:31:29.800 回答