查看Google Chrome控制台中所有JavaScript变量的列表

时间:2010-05-29 11:22:49

标签: javascript google-chrome google-chrome-devtools

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

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

15 个答案:

答案 0 :(得分:285)

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

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

这将列出window对象上的所有可用内容(所有功能和变量,例如此页面上的$jQuery等)。虽然,这是一个很清单;不确定它有多大帮助...

否则只需执行window并开始向下看树:

window

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

答案 1 :(得分:68)

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

chrome-globals

答案 2 :(得分:53)

打开控制台,然后输入:

  • keys(window)查看变量
  • dir(window)查看对象

答案 3 :(得分:36)

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

chrome-show-all-variables-expand-window-object

答案 4 :(得分:29)

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

(function(){var standardGlobals=["top","window","location","external","chrome","document","inlineCSS","target","width","height","canvas","data","DOMURL","img","svg","ctx","url","w","a","speechSynthesis","webkitNotifications","localStorage","sessionStorage","applicationCache","webkitStorageInfo","indexedDB","webkitIndexedDB","crypto","CSS","performance","console","devicePixelRatio","styleMedia","parent","opener","frames","self","defaultstatus","defaultStatus","status","name","length","closed","pageYOffset","pageXOffset","scrollY","scrollX","screenTop","screenLeft","screenY","screenX","innerWidth","innerHeight","outerWidth","outerHeight","offscreenBuffering","frameElement","clientInformation","navigator","toolbar","statusbar","scrollbars","personalbar","menubar","locationbar","history","screen","postMessage","close","blur","focus","ondeviceorientation","ondevicemotion","onunload","onstorage","onresize","onpopstate","onpageshow","onpagehide","ononline","onoffline","onmessage","onhashchange","onbeforeunload","onwaiting","onvolumechange","ontimeupdate","onsuspend","onsubmit","onstalled","onshow","onselect","onseeking","onseeked","onscroll","onreset","onratechange","onprogress","onplaying","onplay","onpause","onmousewheel","onmouseup","onmouseover","onmouseout","onmousemove","onmouseleave","onmouseenter","onmousedown","onloadstart","onloadedmetadata","onloadeddata","onload","onkeyup","onkeypress","onkeydown","oninvalid","oninput","onfocus","onerror","onended","onemptied","ondurationchange","ondrop","ondragstart","ondragover","ondragleave","ondragenter","ondragend","ondrag","ondblclick","oncuechange","oncontextmenu","onclose","onclick","onchange","oncanplaythrough","oncanplay","oncancel","onblur","onabort","onwheel","onwebkittransitionend","onwebkitanimationstart","onwebkitanimationiteration","onwebkitanimationend","ontransitionend","onsearch","getSelection","print","stop","open","showModalDialog","alert","confirm","prompt","find","scrollBy","scrollTo","scroll","moveBy","moveTo","resizeBy","resizeTo","matchMedia","requestAnimationFrame","cancelAnimationFrame","webkitRequestAnimationFrame","webkitCancelAnimationFrame","webkitCancelRequestAnimationFrame","captureEvents","releaseEvents","atob","btoa","setTimeout","clearTimeout","setInterval","clearInterval","TEMPORARY","PERSISTENT","getComputedStyle","getMatchedCSSRules","webkitConvertPointFromPageToNode","webkitConvertPointFromNodeToPage","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","openDatabase","addEventListener","removeEventListener","dispatchEvent"];
    var appSpecificGlobals={};
    for (var w in window){
        if (standardGlobals.indexOf(w)==-1) appSpecificGlobals[w]=window[w];
    }
    console.log(appSpecificGlobals);})()

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

javascript:(function(){var standardGlobals=["top","window","location","external","chrome","document","inlineCSS","target","width","height","canvas","data","DOMURL","img","svg","ctx","url","w","a","speechSynthesis","webkitNotifications","localStorage","sessionStorage","applicationCache","webkitStorageInfo","indexedDB","webkitIndexedDB","crypto","CSS","performance","console","devicePixelRatio","styleMedia","parent","opener","frames","self","defaultstatus","defaultStatus","status","name","length","closed","pageYOffset","pageXOffset","scrollY","scrollX","screenTop","screenLeft","screenY","screenX","innerWidth","innerHeight","outerWidth","outerHeight","offscreenBuffering","frameElement","clientInformation","navigator","toolbar","statusbar","scrollbars","personalbar","menubar","locationbar","history","screen","postMessage","close","blur","focus","ondeviceorientation","ondevicemotion","onunload","onstorage","onresize","onpopstate","onpageshow","onpagehide","ononline","onoffline","onmessage","onhashchange","onbeforeunload","onwaiting","onvolumechange","ontimeupdate","onsuspend","onsubmit","onstalled","onshow","onselect","onseeking","onseeked","onscroll","onreset","onratechange","onprogress","onplaying","onplay","onpause","onmousewheel","onmouseup","onmouseover","onmouseout","onmousemove","onmouseleave","onmouseenter","onmousedown","onloadstart","onloadedmetadata","onloadeddata","onload","onkeyup","onkeypress","onkeydown","oninvalid","oninput","onfocus","onerror","onended","onemptied","ondurationchange","ondrop","ondragstart","ondragover","ondragleave","ondragenter","ondragend","ondrag","ondblclick","oncuechange","oncontextmenu","onclose","onclick","onchange","oncanplaythrough","oncanplay","oncancel","onblur","onabort","onwheel","onwebkittransitionend","onwebkitanimationstart","onwebkitanimationiteration","onwebkitanimationend","ontransitionend","onsearch","getSelection","print","stop","open","showModalDialog","alert","confirm","prompt","find","scrollBy","scrollTo","scroll","moveBy","moveTo","resizeBy","resizeTo","matchMedia","requestAnimationFrame","cancelAnimationFrame","webkitRequestAnimationFrame","webkitCancelAnimationFrame","webkitCancelRequestAnimationFrame","captureEvents","releaseEvents","atob","btoa","setTimeout","clearTimeout","setInterval","clearInterval","TEMPORARY","PERSISTENT","getComputedStyle","getMatchedCSSRules","webkitConvertPointFromPageToNode","webkitConvertPointFromNodeToPage","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","openDatabase","addEventListener","removeEventListener","dispatchEvent"]; var $appSpecificGlobals={};for (var w in window){if (standardGlobals.indexOf(w)==-1) $appSpecificGlobals[w]=window[w];} window.$appSpecificGlobals=$appSpecificGlobals;console.log(window.$appSpecificGlobals);})()

答案 5 :(得分:7)

David Walsh为此提供了一个很好的解决方案。以下是我对此的看法,将他的解决方案与此线程中发现的内容相结合。

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

答案 6 :(得分:4)

您可能需要针对Chrome试用这个Firebug lite扩展程序。

答案 7 :(得分:3)

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

答案 8 :(得分:3)

来自同一article Avindra的更新方法 - 注入iframe并将其$ echo "first_second_third_mmddyyy.csv" | awk -F '_' 'NF{NF-=1};1' first second third 属性与全局窗口属性进行比较。



contentWindow




答案 9 :(得分:3)

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

key name    level   hasData fields  id
1000000 Aelf    0   true    {'id': 'elf'}   elf

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

说句公道话,您会在debugger 范围内得到一切,包括内置的浏览器,因此可能有点像大海捞针。 :/

答案 10 :(得分:2)

在控制台中输入:<form method="post"> //I also used here action attribute. Stuff Name:<input type="text" name="name" id="name"><br><br> ....... so on.... <input type="submit" value="Submit Data"> </form> <?php if(isset($_POST['submit'])){ global $wpdb; $name = $_POST['name']; $f_name = $_POST['f_name']; ...... so on..... $user_details = array('name' => $name, 'f_name' => $f_name, 'm_name' => $m_name, 'b_date' => $b_date, 'gender' => $gender, 'b_group' => $b_group, 'photo' => $photo, 'email' => $email, 'phone' => $phone, 'present_address' => $present_address, 'permanent_address' => $permanent_address, 'nid' => $nid, 'exp' => $exp, 'at' => $at, 'other_file' => $other_file, 'al' => $al, 'u_name' => $u_name, 'password' => $password, 'd_name'=> $d_name, 'in_time' => $in_time, 'out_time' => $out_time, 'j_date' => $j_date, 'salary' => $salary); $table_name =$wpdb->prefix."stuff"; $result = $wpdb->insert($table_name, $user_details, array('%s','%s','%s','%s','%s','%s','%b','%s','%s','%s','%s','%b','%b','%b','%b','%b','%s','%s','%s','%s','%s','%s','%i')); }; ?>

要获取我认为的this,我认为它与在控制台中键入window object基本相同。

它至少在Firefox和chrome中有效。

答案 11 :(得分:1)

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

答案 12 :(得分:1)

enter image description here

答案 13 :(得分:1)

Try this simple command:

console.log(window)

答案 14 :(得分:0)

我结束使用它进行调试:

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

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