Explain Codes LogoExplain Codes Logo

Best Way to View Generated Source of a Webpage?

html
dom-inspection
javascript
devtools
Nikita BarsukovbyNikita Barsukov·Jan 30, 2025
TLDR

Immediate solution: Make use of Browser DevTools to rapidly inspect a page's dynamic HTML:

  • Option-click, pick Inspect or use the shortcut Ctrl+Shift+I/Cmd+Option+I.
  • Navigate to the Elements tab for updates on the HTML and DOM in real-time.

To spot the source code in a new tab, this code snippet comes in handy:

let sourceCode = `data:text/html;charset=utf-8,${encodeURIComponent(document.documentElement.outerHTML)}`; window.open(sourceCode);

(Laughing in DOM) This code pinpoints the webpage's live HTML, enabling you to scrutinize the modified structure once the scripts have been executed.

Core Concepts and Tools

Notably, HTML is considered a language for document description, that the browser reads and processes subsequently. Any tool aiming to display HTML, relies on the document's content for its validity check.

To modify the document using JavaScript, the best practice involves implementing document methods. This not only secures control programmatically but also keeps intact the document's integrity. While making changes, when document.write, innerHTML or other similar methods are utilized, validating the HTML input distinctly is crucial.

For a byte-level precise version of the HTML dispatched by the server, rely on Fiddler. It presents intact HTML bytes, making it perfect for an unaltered source analysis.

For users of Firefox, the Firebug add-on avails features with minimal alterations, and is more suited to examining AJAX-modified HTML.

Developer Techniques: Source Manipulation

When viewing source code, you must be aware that any alterations to the document using JavaScript should be validated through methods such as document.write or innerHTML. This not only ensures programmatic control but also maintains the document's integrity.

As a fast way to access the exact HTML representation, make use of document.documentElement.innerHTML within your browser's console. It serves the purpose of revealing the current status of the document.

For Firefox users, the Firebug add-on modifies the HTML minimally, which makes it ideal for inspecting HTML that has been modified by AJAX calls.

If you need a byte-by-byte representation of the HTML sent by the server, consider Fiddler. It captures the exact HTML bytes transmitted over the network, which is ideal for unaltered source analysis.

Developer Techniques: DOM Inspection

For inspecting and validating JS-generated HTML, use Chrome's DevTools with Elements view unfolded to see the full DOM tree. Alternatively, you can use the "Validate Local HTML" function of Web Developer Toolbar to validate the current page source without requiring an internet connection.

For pages with AJAX calls, you may need to manually combine the original HTML with the AJAX HTML. The Fiddler tool can help you perform this operation by capturing HTTP traffic and allowing you to reconstruct the full HTML.

Finding Your Way with Dynamic Content

Dynamic content usually involves JavaScript modifying the HTML after it loads. Understanding how to view this generated content can be esseantial when debugging.

Here are some useful observations:

  • document.documentElement.innerHTML allows quick access to the HTML representation of the document.
  • The Network tab in DevTools is invaluable for checking AJAX calls and the resulting DOM changes.
  • Browser extensions, like Wappalyzer, offer insight into the website's technologies, adding more context while viewing the source.

Avoiding Common Mistakes

Here's some advice to avoid common pitfalls:

  • DevTools might ignore some content created by scripts until you interact with the DOM. Explore the page fully to discover all elements.
  • The data you see in DevTools is a representation of client-side rendering, which may differ from the server-side HTML delivery.
  • Some actions like injecting breakpoints or exploring the DOM extensively can affect page performance. Use these judicially.