Explain Codes LogoExplain Codes Logo

What is the difference between srcdoc="..." and src="data:text/html,..." in an ?

html
iframe
security
best-practices
Alex KataevbyAlex Kataev·Dec 13, 2024
TLDR

**srcdoc** directly embeds HTML within an <iframe>, bringing simplicity and readability to your inline content. On the other hand, **src** having a **data URI** encodes the HTML into a URL. Neat for small data, yet clumsy for larger content. Betting on srcdoc means betting on HTML clarity:

srcdoc:

<iframe srcdoc="&lt;p&gt;Hello, World!&lt;/p&gt;"></iframe>

data URI:

<iframe src="data:text/html,&lt;p&gt;Hello, World!&lt;/p&gt;"></iframe>

Same Hello, World!, different approach. srcdoc simply does it without hurdles, the perfect choice for HTML inside iframes.

When dealing with untrusted content, **srcdoc** snuggles safely with the **sandbox** attribute, becoming a security haven. However, tread carefully, as legacy browsers might not sit well with **srcdoc**. If **src** and **srcdoc** are provided, **srcdoc**'s the boss. It's prepped to supply internal content without external URL fetches.

Caught with Internet Explorer 11? Be aware, no **srcdoc** support there. But, modern browsers love **srcdoc**, making it a safer route for inline content. **src** with data URLs can invite cross-domain party-crashers and be caged by URL length limits.

Inside the content box – advanced handling

Seamless inlining and handling cross domains

**srcdoc** scores with its seamless inline embedding, ditching URL-coded HTML for better readability and maintenance. Create a whole HTML world in one iframe, bypassing cross-domain concerns that arise with **src** and data URIs:

srcdoc embedding:

<iframe srcdoc="&lt;style&gt;h2 {color: red;}&lt;/style&gt;&lt;h2&gt;Styled Content&lt;/h2&gt;"></iframe>

// It's so easy, it feels like cheating! 😅

Playing with cookies

Cookies and **iframes** can be a finicky duo. **srcdoc** breaks down barriers, allowing stable cookie handling. Unlike **src** with data URIs, which operate in their secure bubble. Beware of cookie restrictions across different domains — **srcdoc**’s got your back, all under the same origin.

Dealing with browser quirks

Broad browser support for **srcdoc** doesn't mean quirks are out of the game. Chrome loves cookies, but it has its own rules. **srcdoc** knows the drill. **src** can face browser-specific restrictions, length limitations, and more fun complications.

Unsupported browser? Here are alternatives

Stuck with prehistoric browsers not supporting **srcdoc**? Consider structured data passing methods like postMessage or server-sided solutions to push content securely into iframes. Yes, they're more complex, but they deliver on security and interactivity.

An approach for large content

Avoid overcrowding your **src** attribute's data URI, as it can become unmanagebly long. For chunky HTML documents, **srcdoc** offers a better run by skipping the rigmarole of encoding and decoding hefty data URLs.

Speedy loading and UX

In **srcdoc**, the iframe’s content is part of the initial page load, potentially sprinting your page speed metrics. Exploit **srcdoc** for inline content that should load fast and furious, thus boosting user experience.

Debugging and maintenance

Maintaining and debugging get simpler with **srcdoc** because it lets developers edit HTML within the parent document itself. This easy steering results in a smoother development cycle, in contrast to dealing with encoded data URIs, which can be like solving a mystery. // Who doesn't love a bit of hide and seek, right?