srcdoc ="..."和"..."之间有什么区别?和src =" data:text/html,..."在< iframe>中? [英] What is the difference between srcdoc="..." and src="data:text/html,..." in an <iframe>?

查看:35
本文介绍了srcdoc ="..."和"..."之间有什么区别?和src =" data:text/html,..."在< iframe>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这是它们之间的区别:

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>

演示

而且,如果它们完全相同,为什么HTML5会添加 srcdoc 属性?

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

修改

也许我还不够清楚.我不是将 src srcdoc 进行比较,而是将 src 与text/html数据URI和 srcdoc 一起使用.

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

为什么需要 srcdoc ?

(*)注意:

似乎 srcdoc 可用于通过URL加载页面(演示),使用带有 src 属性的子框架:

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>

推荐答案

其他答案列出了一些表面上的区别,但实际上错过了解释为什么浏览器/规范编写者本质上会解释的关键区别的标记复制已经存在的东西:

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 =数据:...不受信任的内容"沙箱/> <-在现代浏览器中是安全的,在没有沙箱的旧版浏览器中是不安全支持

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

< iframe srcdoc ="...不受信任的内容"沙箱/> <-在现代浏览器中是安全的,在旧版本中是安全(尽管不起作用)浏览器

<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.

这篇关于srcdoc ="..."和"..."之间有什么区别?和src =" data:text/html,..."在&lt; iframe&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆