如何在 IE8 中转储 JavaScript 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1956384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:04:10  来源:igfitidea点击:

How do I dump JavaScript vars in IE8?

javascriptinternet-explorer-8consolefirebugie-developer-tools

提问by Pekka

I have an object I need to examine in IE8. I tried the developer tools and console.log, their Firebug equivalent. However, when I output the object to the log:

我有一个需要在 IE8 中检查的对象。我尝试了开发者工具和console.log它们的 Firebug 等价物。但是,当我将对象输出到日志时:

console.log("Element: ", element);
console.log(element);

I only get the string

我只得到字符串

LOG: Element: [object Object]

instead of a clickable, examinable dump.

而不是可点击、可检查的转储。

Is it possible to dump an object to the Log and examine its members, like in Firebug?

是否可以将对象转储到日志并检查其成员,就像在 Firebug 中一样?

I can't use a homemade dump() function because the element I want to examine is so huge the browser will crash on me.

我不能使用自制的 dump() 函数,因为我要检查的元素太大了,浏览器会在我身上崩溃。

回答by Xavi

Here's one technique that I've found helpful:

这是我发现有用的一种技术:

  • Open the Developer Tool Bar (hit F12)
  • Go to the "Script" tab
  • Click the "Start Debugging" button
  • Next, type "debugger" into the console and hit enter. This should trigger a break point.
  • Go to the "Watch" sub-tab
  • Click the row that says, "Click to add..." and enter a variable you'd like to examine. Note that the variable must be globally available.
  • At this point you should be able to examine your variable with tree-like UI
  • Once you're done debugging click Continue button (or hit F5)
  • 打开开发者工具栏(按 F12)
  • 转到“脚本”选项卡
  • 单击“开始调试”按钮
  • 接下来,在控制台中输入“debugger”并按回车键。这应该会触发一个断点。
  • 转到“观看”子选项卡
  • 单击显示“单击以添加...”的行,然后输入要检查的变量。请注意,该变量必须是全局可用的。
  • 此时,您应该能够使用树状 UI 检查您的变量
  • 完成调试后,单击“继续”按钮(或按 F5)

回答by Andy

A bit off topic (as it won't work for DOM elements) but I've found it handy to use the JSON.stringify(object) to get a JSON string for the object which is pretty readable.

有点离题(因为它不适用于 DOM 元素),但我发现使用JSON.stringify(object) 来获取非常易读的对象的 JSON 字符串很方便。

回答by René

@Chris commented @Andy's answer with the simple solution: Use console.dir(myObj)to get all the details printed out in the console in IE. Thanks Chris!

@Chris 用简单的解决方案评论了@Andy 的答案:用于console.dir(myObj)在 IE 的控制台中打印出所有详细信息。谢谢克里斯!

回答by Cees Timmerman

If you're dealing with nasty code and console.log is not available, try this in the console:

如果您正在处理讨厌的代码并且 console.log 不可用,请在控制台中尝试以下操作:

out = []; for (i in your_object) { out.push(i) } out.join("\n")

回答by winladen

One suggestion is to use Firebug-Lite: It wraps console obj and you can see the result in IE like in most of the firebug console. Hope this help.

一个建议是使用Firebug-Lite:它包装控制台 obj,您可以在 IE 中看到结果,就像在大多数 firebug 控制台中一样。希望这有帮助。

回答by Sharad

Add this Tag in your page :

在您的页面中添加此标签:

<script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script>

And the things will work.

事情会奏效的。

Its working on my system.

它在我的系统上工作。

Note: Do try this solution.

注意:请尝试此解决方案。

回答by Frison Alexander

A pictorial version of Xavi's excellent answer:

哈维出色回答的图片版本:

enter image description here

在此处输入图片说明

回答by dsuess

Dump it into an existing HMTL-Element

将其转储到现有的 HMTL 元素中

I noticed IE 11 is stripping off console lines after 1027 chars :-/ When I had a large object to dump (12,000 chars) I dumped it into an existing DIV- oder TextArea-Element, from where I could copy the content.

我注意到 IE 11 在 1027 个字符后剥离控制台行:-/ 当我有一个大对象要转储(12,000 个字符)时,我将它转储到现有的 DIV-oder TextArea-Element 中,从那里我可以复制内容。

var str = JSON.stringify(myObject);
$('#existing-element').text(str); // jQuery or
document.querySelector("#existing-element").innerHTML = str; // native JavaScript

回答by Carnix

I know this is a REALLY old question, but I was looking for an answer to this just now. If it's not an absolute requirement to use the IE console (which isn't very good, IMO), then you might consider using Firebug Lite (http://getfirebug.com/firebuglite). It's not a perfect solution, and you may not want to push that script out to your production environment, and it's not as full featured as Firebug, but it's pretty good in a pinch when you have to much around with a low-end browser like IE.

我知道这是一个非常古老的问题,但我刚才正在寻找答案。如果不是绝对需要使用 IE 控制台(这不是很好,IMO),那么您可以考虑使用 Firebug Lite ( http://getfirebug.com/firebuglite)。这不是一个完美的解决方案,您可能不想将该脚本推送到您的生产环境中,而且它的功能不如 Firebug 完整,但是当您不得不使用像这样的低端浏览器时,它非常适合IE。

回答by br4nnigan

A bit chunky but it works for DOM objects:

有点笨重,但它适用于 DOM 对象:

 console.log( testNode.outerHTML.replace(testNode.innerHTML,"") );