98

For example, which is the difference between these:

<iframe srcdoc="<p>Some HTML</p>"></iframe>
<iframe src="data:text/html,<p>Some HTML</p>"></iframe>

Demo

And, in case they are exactly the same, why did HTML5 add srcdoc attribute?

Edit

Maybe I wasn't clear enough. I am not comparing src with srcdoc, but src using text/html data URI with srcdoc.

Then, if the functionality chart is like this

                   |  src attribute       |  srcdoc attribute
 --------------------------------------------------------------------
  URL              |  Yes                 |  No without using src (*)
  HTML content     |  Yes, using data URI |  Yes

why is srcdoc needed?


(*) Note:

It seems srcdoc can be used to load a page by URL (Demo), using a subiframe with srcattribute:

<iframe srcdoc="<iframe src='http://microsoft.com'></iframe>"></iframe>

9 Answers 9

70

The other answers list some superficial differences, but really miss the mark of the key difference that explains why browsers/spec writers would essentially duplicate something that already exists:

<iframe src="data:...untrusted content" sandbox /> <- Secure in modern browsers, insecure in legacy browsers with no sandbox support

<iframe srcdoc="...untrusted content" sandbox /> <- Secure in modern browsers, secure (though non-functional) in legacy browsers

This new syntax provides content authors a way to protect their users, even when they may be using legacy browsers. Without it, content authors would be reluctant to use the sandbox feature at all, and it would not see use.

5
  • If the main benefit is untrusted content, why spec it to contain the content inline — doesn't most untrusted content come from external URLs? I.e. why not sandboxedsrc attribute that takes a [data] URI? (It'd also avoid some future browser from implementing srcdoc without implementing sandbox). Commented Dec 9, 2015 at 9:38
  • 1
    Because authors can already use external urls (on a separate domain) to serve untrusted content. Commented Dec 10, 2015 at 17:45
  • What do you mean by secure? It can’t access the embedding page? The embedding paye can’t access it? Or both? Or what Commented Jun 30, 2019 at 16:59
  • Otherwise What is the point of this over a regular div or HTML component Commented Jun 30, 2019 at 17:01
  • Any article on iframe's "sandbox" will discuss what benefits it provides Commented Jul 1, 2019 at 17:14
23

iframe with src attribute with HTML Content is cross domain. This is true even with data URLs as mentioned here

data: URLs get a new, empty, security context.

But iframe with srcdoc attribute with HTML Content is not cross domain.

So using the src attribute might cause errors if you try to access/manipulate the iframe from the parent page:

Blocked a frame with origin "..." from accessing a cross-origin frame.

3
  • 3
    That's a good point, Chrome treats data URIs as cross domain. Firefox treats them as same origin, not sure what Edge does.
    – Oriol
    Commented Jan 23, 2017 at 15:59
  • 2
    What do you mean by “not cross domain”? Commented Jun 30, 2019 at 16:52
  • 2
    @GregoryMagarshak "Not cross-domain", meaning the iframe content will be treated as coming from same domain as the parent-page. Thus, it won't be restricted by browser same-origin policy, which normally prevents browser from accessing content of the remote page, as it would be when you use src='some-url.com'. developer.mozilla.org/en-US/docs/Web/Security/…
    – johny why
    Commented Apr 5, 2020 at 17:21
23

From MDN documentation on the srcdoc attribute :

Inline HTML to embed, overriding the src attribute. If a browser does not support the srcdoc attribute, it will fall back to the URL in the src attribute.

Older MDN documentation on srcdoc says:

The content of the page that the embedded context is to contain. This attribute is expected to be used together with the sandbox and seamless attributes.

So, the srcdoc attribute overrides the content embedded using src attribute.

Demo

(Note: The seamless attribute has since been removed, and another answer describes how sandbox attribute should be "used with the srcdoc attribute"; both attributes were introduced in HTML5; sandbox should be used in case the srcdoc contains untrusted content.


Also, what you are saying about the following snippet data:text/html is called data URL and it might have length limitations, so src is limited in what it can show, whereas srcdoc is less limited.

MSDN mentions length limitations:

Data URIs cannot be larger than 32,768 characters.

But other browsers might have different data URL length limits (higher limits) on data URLs themselves (see Chromium might allow 512 URLs), but still not allow URLs to exceed 2MB

8
  • "So, the srcdoc attribute overrides the content embedded using src attribute." Apparently not in Firefox 24, I see two IFrames with Microsoft's website in it. Commented Nov 2, 2013 at 15:49
  • @MarcelKorpel The first version of Firefox which supports srcdoc is 25 (developer.mozilla.org/en-US/docs/Web/HTML/Element/…)
    – Oriol
    Commented Nov 2, 2013 at 15:51
  • 2
    @MrAlien Good point about the length limitation of data URIs, unlike html attributes which have no limit. Anyway, it seems that limit is imposed by Microsoft implementation, because MDN don't say anything about a limit, and the RFC standard only says "Note that some applications that use URLs may impose a length limit"
    – Oriol
    Commented Nov 2, 2013 at 15:57
  • 1
    The reason why srcdoc is necessary (as implied, but not explicitly stated, in this answer) is that data URIs are not universally supported for HTML content. This means that a browser which does not support a data URI in src has no graceful fallback method to display the content. However, using srcdoc with an http URI in src which resolves to the same embedded content does provide browsers with a graceful degradation option.
    – daiscog
    Commented Sep 10, 2014 at 9:52
  • 3
    Is that data URI limit real? HTMLCanvasElement.toDataURL regularly returns dataURLs larger than 32k
    – gman
    Commented Jul 9, 2019 at 17:31
15

As of writing - srcdoc in Chrome (v36) allows the setting and fetching of cookies, whereas the use of src with data URL does not:

Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs

This may or may not be important to you, but rules out the use of data URLs in the application I am building, which is a shame, as of course IE doesn't support srcdoc currently (v11).

2
  • 1
    Good point. Data URIs have limitations on some browsers, so srcdoc works better in those cases.
    – Oriol
    Commented Aug 2, 2014 at 22:04
  • In Chromium 81 parent frame background and font styles also cascade into the iframe, adding even more utility.
    – vhs
    Commented May 6, 2020 at 15:32
9

Another noticeable difference is that src attributes with data-uri support URI percent-encoding rules while srcdoc doesn't as it supports regular html syntax,

these sources will yield differently:

<iframe srcdoc="<p>hello%20world</p><h1>give%0D%0Ame%0D%0Asome%24</h1>"></iframe>

<iframe src="data:text/html;charset=UTF-8,<p>hello%20datauri<p><h1>give%0D%0A me%0D%0Asome%24</h1>"></iframe>

I also noticed a difference in the parsing of js scripts inside the attributes value( it's probably more than just percentage-encoding ) but didn't figure the rule yet...

2
  • 1
    The content of src has to be URL-encoded to be treated correctly. See this question for details on how to do that.
    – user
    Commented Jul 9, 2017 at 19:15
  • I didn't have much luck URI encoding attribute values on larger textfiles. What worked for me was to sanitize input using recode(1) in bash beforehand.
    – vhs
    Commented May 6, 2020 at 15:36
1

Another difference is how links to fragments inside the iframe HTML work. Let's say your HTML content has a link/<a> pointing to a document fragment/hash/id somewhere in the same page, like this:

<a href="#in_src"></a>

<!-- lots of other content, requiring scrolling -->

<p id="in_src"></p>
  • If you use the src attribute, (example on jsfiddle, or jsbin) the same link will scroll/jump you to the relevant fragment (as I expected).

  • If you use the srcdoc attribute, (example on jsfiddle, or jsbin) a fragment link like the above will try to re-navigate the entire iframe to a whole new page, (based on the parent page's context? I'm not sure, but it's not what I expected)

Maybe this is because of the difference in domains in srcdoc vs src? Maybe the difference in their baseURI values (see more below?). I'm not sure

I tested this on Chrome Version 111.0.5563.149 (Official Build) (64-bit)

EDIT

I wanted to the link to scroll/jump to the document fragment, even with a srcdoc attribute. So in this example I used Javascript to...

  • addEventListener() for the click event
  • event.preventDefault() (to prevent the anchor's iframe-resetting behavior)
  • and use location.hash= '#in_src'; to manually jump to the doc fragment.

Note that the values of e.target.attributes.href.value and e.target.href do not necessarily have the same value (i.e. the value of the fragment ''#in_src'`)

  • The attributes.href.value is the literal string value of the href attribute, like '#in_src'
  • But the href property of the HTMLAnchorElement API has a "a string [value] containing the whole URL"
    • in the case of srcdoc, the href value is something like 'https://...#i01' (the baseURI value is the parent's URL (iframeWindow.parent.document.baseURI), and maybe that's why clicking the link will navigate the whole iframe)
    • in the case of src using a data URI, the href value is something like 'data:text/html,...#i01, (the baseURI value is the data URI, and will jump to the fragment instead of navigating the hwole page)
-1

In your example the two forms are functionally identical. However, you can use both src and srcdoc attributes, allowing non-HTML5 browsers to use the src version, while HTML5 browsers can use the srcdoc version along with the sandbox and seamless attributes which offer more flexibility in how an iFrame is treated.

2
  • But sandbox and seamless attributes can be used too with src attribute, can't they? It seems to me that it's src which is more flexible than srcdoc
    – Oriol
    Commented Nov 2, 2013 at 16:00
  • @Oriol, I think my answer goes directly to why this is important, not as a flaw, but as a feature Commented Jul 7, 2015 at 19:19
-4

srcdoc: The content of the page that the embedded context is to contain. This attribute is expected to be used together with the sandbox and seamless attributes. If a browser supports the srcdoc attribute, it will override the content specified in the src attribute (if present). If a browser does NOT support the srcdoc attribute, it will show the file specified in the src attribute instead (if present).

src: The URL of the page to embed.

1
  • 9
    But src attribute can contain the HTML content of the page too, using data URIs
    – Oriol
    Commented Nov 2, 2013 at 16:01
-4

The main difference is that the 'src' attribute contains the address of the document you are going to embed in the tag.

On the other hand 'srcdoc'attribute contains the HTML content of the page to show in the inline frame.

the main disadvantage of srcdoc is that it is not supported in all browsers whereas src is compatible with all the browsers.

for detailed explanation please go through the following link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe

1
  • 9
    But src attribute can contain the HTML content of the page too, using data URIs
    – Oriol
    Commented Nov 2, 2013 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.