在窗口[]、html/javascript中显示变量(或所有内容)的名称

在窗口[]、html/javascript中显示变量(或所有内容)的名称,javascript,html,dom,variables,window,Javascript,Html,Dom,Variables,Window,我试图在HTML页面中制作一个穷人的萤火虫。我创建了一个外部脚本,放在目标页面的某个地方。用初始化。它还引入了一个样式表来绝对地定位一个带有结果的部分不透明的表 因此它在window[]中遍历所有内容(值),在一列中显示它,在另一列中显示typeof(window[i])。我还希望有另一列显示变量或对象的名称 function monitor_init() { var css = document.createElement("link"); css.setAttribute("

我试图在HTML页面中制作一个穷人的萤火虫。我创建了一个外部脚本,放在目标页面的某个地方。用
初始化。它还引入了一个样式表来绝对地定位一个带有结果的部分不透明的表

因此它在
window[]
中遍历所有内容(值),在一列中显示它,在另一列中显示
typeof(window[i])
。我还希望有另一列显示变量或对象的名称

function monitor_init()
{
    var css = document.createElement("link");
    css.setAttribute("href","jsmonitor.css");
    css.setAttribute("rel","stylesheet");
    css.setAttribute("type","text/css");
    document.getElementsByTagName("head")[0].appendChild(css);
    var temp = document.createElement("div");
    temp.setAttribute("id","monitor_box");
    var temp2 = document.createElement("table");
    temp2.setAttribute("id","monitor_output");
    temp.appendChild(temp2);
    document.body.appendChild(temp);
    monitor();
}

var monitor_speed = 100;

function monitor()
{
    while(document.getElementById("monitor_output").childNodes.length > 0)
    {
        document.getElementById("monitor_output").removeChild(document.getElementById("monitor_output").lastChild);
    }
    for (i in window)
    {
        if (["function","undefined"].indexOf(typeof(window[i]))!=-1)
        {
            continue;
        }
        // This is where I tried to make a first column displaying the name, couldn't find anything that worked.
        // Disregard the .name, that was just last-ditch stuff
        //var temp = document.createElement("tr");
        //var temp2 = document.createElement("td");
        //temp2.appendChild(document.createTextNode(window[i].name));
        //temp.appendChild(temp2);

        var temp = document.createElement("tr");
        var temp2 = document.createElement("td");
        temp2.appendChild(document.createTextNode(typeof(window[i])));
        temp.appendChild(temp2);

        var temp2 = document.createElement("td");
        temp2.appendChild(document.createTextNode(window[i]));
        temp.appendChild(temp2);
        document.getElementById("monitor_output").appendChild(temp);
    }
    setTimeout("monitor()",monitor_speed);
}

通过使用typeof,我可以跳过显示函数值和一些未定义的内容。我希望如果我可以访问这个名称,我也可以跳过历史记录、位置和其他非JavaScript数字、字符串、数组对象等内容,即我在页面上的其他脚本中创建的全局变量。

您使用的循环结构将提供以下信息:

for (i in window) {
    // at the point, i is a string containing the name of the property
    // window[i] contains the value of that property
    document.write('window.' + i + ' is ' + window[i] + '<br>');
}
for(窗口中的i){
//此时,i是一个包含属性名称的字符串
//窗口[i]包含该属性的值
document.write('window.'+i+'是'+window[i]+'
'); }

但正如另一张海报所问——为什么不能使用firebug lite?

您并没有真正提出一个具体的问题,但我想您是想说“如何获得
窗口的每个属性的名称?”

在现有代码中,使用for-In循环遍历各种属性。我认为您需要知道的是,索引变量将包含(当前)属性的名称:

for (i in window) {
  alert("Property name: " + i);
  alert("Property value: " + window[i]);
}

有什么原因不能直接使用Firegut Lite吗?我想在这个简单功能的基础上尝试一些更高级、更具体的跟踪。谢谢,谢谢。不知何故,我忘记了“I”包含关联数组的每个键。