简体   繁体   English

如何查看 JavaScript 渲染的源代码?

[英]How can I see JavaScript-rendered source?

I am trying to see the HTML that's created by a JavaScript snippet on my HTML page by viewing the page source.我试图通过查看页面源来查看由我的 HTML 页面上的 JavaScript 片段创建的 HTML。 I don't want to use any add-on programs.我不想使用任何附加程序。 Is this possible through programming?这可以通过编程实现吗?

In firefox, do ctrl/command-a to select all, right click and do "view selection source."在 firefox 中,执行 ctrl/command-a 全选,右键单击并执行“查看选择源”。 That'll show the DOM for the page as it currently is rather than just whatever HTML and JS it started from.这将显示当前页面的 DOM,而不仅仅是它开始的任何 HTML 和 JS。

只需查看 DOM 中任何对象的innerHTML

Here is my favorite solution in IE (version 10):这是我最喜欢的 IE 解决方案(版本 10):

1) Press F12 key to launch Developer Tools 1) 按 F12 键启动开发者工具
2) Click on the "Console" tab 2) 单击“控制台”选项卡
3) In prompt at the bottom, enter: 3)在底部的提示中,输入:

document.body.innerHTML

Now it should display in the current window the dynamic, current, generated html.现在它应该在当前窗口中显示动态的、当前的、生成的 html。 (To look at the html head section instead you can replace the word 'body' with 'head'.) (要查看 html head 部分,您可以将单词 'body' 替换为 'head'。)

To do this with Safari 13 (2019) on the Mac:要在 Mac 上使用Safari 13 (2019)执行此操作:

  1. Ensure that the Develop menu is enabled (Preferences > Advanced > Show Developer Menu in menu bar).确保启用了开发菜单(首选项 > 高级 > 在菜单栏中显示开发者菜单)。
  2. Select Show Web Inspector from the Develop menu.从“开发”菜单中选择“显示 Web Inspector ”。 The first time you do this the option Resources will typically be selected, showing you the un-rendered HTML source.第一次执行此操作时,通常会选择资源选项,显示未渲​​染的HTML 源代码。
  3. Change the selection on the horizontal menu to Elements .将水平菜单上的选择更改为Elements

在 Safari Web Inspector 中选择的元素

  1. Expand the DOM Tree (circled triangle) until you get to your JavaScript instance, which you will see both as its unrendered and rendered version, as in the example below (a link specific for a particular day of the month).展开 DOM 树(带圆圈的三角形),直到到达您的 JavaScript 实例,您将看到它的未渲染版本和渲染版本,如下例所示(特定于一个月中特定日期的链接)。

未渲​​染和渲染版本的 JavaScript 示例

No plugin or additional software required (and some other nice tools there too).不需要插件或额外的软件(还有其他一些不错的工具)。

No. You need a tool that monitors changes to the DOM.不可以。您需要一个工具来监控 DOM 的变化。 Browsers will only show you the mark-up unaffected by JavaScript or any other client-side programming.浏览器只会向您显示不受 JavaScript 或任何其他客户端编程影响的标记。

Use WebKit's Web Inspector or other similar tool.使用 WebKit 的 Web Inspector 或其他类似工具。

Press F12 in most browsers to bring up the developer tools.在大多数浏览器中按 F12 以调出开发人员工具。

Search for tutorials for the various developer tools of the browser of your choice.搜索您选择的浏览器的各种开发人员工具的教程。

I'm 8 years late but I made a Chrome extension to do just this:我迟到了 8 年,但我制作了一个 Chrome 扩展程序来做到这一点:

View Rendered Source 查看渲染源

It shows the raw HTML and the rendered DOM, and compares the diff between the two.它显示原始 HTML 和渲染的 DOM,并比较两者之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM