空的 iframe src 有效吗?
我希望 iframe 最初将 src 设置为空白,然后在页面加载后;调用 JS 函数,然后将 src
值设置为实际值。
有效还是我需要使用其他类似
javascript:;
等
I want an iframe to initially have src
as blank and then once the page loads; call a JS function and then set the src
value to an actual one..
So is <iframe src="#" />
valid OR do I need to use something else like javascript:;
, etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
只是
just
<iframe src='about:blank'></iframe>
HTML 5 工作草案,第 4.8.2 节,说(强调我的):
根据 RFC 3986,有效的 URL 必须以 方案名称,以及相对引用不能仅存在于片段中。
因此,
#
不是有效的 URL,不应用作src
属性的值。使用 about:blank反而。
The HTML 5 working draft, section 4.8.2, says (emphasis mine):
According to RFC 3986, valid URLs must begin with a scheme name, and relative references cannot only consist in a fragment.
Therefore,
#
is not a valid URL, and should not be used as the value of thesrc
attribute.Use about:blank instead.
不,指定空 iframe src无效。
您应该使用
。
#
旨在对当前页面内的锚点的引用(或者在处理 AJAX 请求时通常用作路由方案)。使用它作为 iframe 的源是没有意义的,因为 iframe 不引用当前页面上的内容,也不与 AJAX 请求一起使用。about:blank
是显示空白文档的跨浏览器标准。2012 年 6 月 8 日更新:
看来 'living' 规范 不再使 iframe 无效:
但是,如果这两个属性均未设置,则浏览上下文将默认为
about:blank
。为了提供适当的向后兼容性,建议提供详细信息,并且目前提供about:blank
URL。No, it is not valid to specify an empty iframe src.
You should use
<iframe src="about:blank" />
.#
is meant to be a reference to an anchor within the current page (or, often used as a routing scheme when working with AJAX requests). Using it as the source of an iframe would be senseless, since an iframe does not reference content on the current page and is not used with AJAX requests.about:blank
is a cross-browser standard to display a blank document.Update June 8th 2012:
It seems that the 'living' spec no longer renders an iframe invalid if the
src
attribute is missing:If both of these attributes, however, are not set, the browsing context will default to
about:blank
. To provide proper backwards compatibility, it's recommendable to be verbose and, for now, provide theabout:blank
URL.看起来您也可以完全省略 src:
http:// /dev.w3.org/html5/spec/Overview.html#the-iframe-element
It looks like you can also leave out the src completely:
http://dev.w3.org/html5/spec/Overview.html#the-iframe-element
如果您不想使用
about:blank
,您可以使用 javascript 作为iframe
的src
,如下例所示:将初始化一个具有栗色背景的
iframe
,其中包含一条简单的消息结果窗口
。但是,链接的目标应等于
iframe
name
属性,即该示例中的TV
。注意:
某些外部网站可能会阻止从其他来源请求其页面。例如,尝试将以下示例中的超链接 URL 更改为
yahoo.com
或google.com
,然后检查浏览器的控制台。Jsbin 示例
If you don't want to use
about:blank
you may use javascript as ansrc
for youriframe
like the following example:The above example will initialize an
iframe
with maroon background that has a simple message<h3>Results Window</h3>
. However, the targets of your links should equals theiframe
name
attribute i.e in that exampleTV
.Notice:
Some external website may block requesting their pages from another origin. Try to change the URLs of the hyperlinks in the example below to
yahoo.com
orgoogle.com
, for example, and checkout your browser's console.Jsbin example
您可以在
src
属性中使用about:blank
(如 ariel 之前提到的),否则从安全页面提供服务时会抛出错误。安全页面
https
会在安全网站上抛出可能不安全数据的错误。You can use
about:blank
in thesrc
attribute (as mentioned by ariel earlier), otherwise it would throw an error when serving from a secure page.A secure page
https
would throw an error of possibly un-secure data on the secure website.您可以尝试通过 Javascript 添加 iframe,这样您就不需要在 HTML 中添加空白 iframe:
(jQuery 示例)
使用 Javascript 添加 iframe 可以实现优雅降级 - 没有 Javascript 的用户不会看到空白 iframe。
You could try adding the iframe through Javascript, so you wouldn't need to have a blank one in the HTML:
(jQuery example)
Adding the iframe with Javascript allows graceful degradation - users without Javascript won't see a blank iframe.
作为 about URI 方案标准的一部分,about:blank 可能是最好的选择,因为它具有非常好的浏览器支持。
As part of the about URI scheme standard, about:blank is probably the best option as it has very good browser support.
为了记录,
让我们说下一个例子(Firefox 58,但可能存在于所有浏览器中)。
Iframe 在 css 之前加载,因此页面的渲染在 css 加载之前可见。也就是说,有那么一瞬间,页面看起来没有 CSS。
相反:
它工作正常,CSS 已加载,iframe 最终已加载。
For the record
Let's say the next example (Firefox 58 but may be its present in all browsers).
Iframe is loaded BEFORE the css so the render of the page is visible before the css is loaded. I.e. for a split of a second, the page looks without a css.
Instead:
It works fine, the css is loaded and the iframe is loaded finally.
如果你确实用
调用你的 iframe
javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(VARIABLES,,true,,false,)
在服务器端你可以再次有一个 src=""
if you do call your iframe with
javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(VARİABLES,,true,,false,)
in server side you can again have a src=""
原始 HTML 和 JavaScript 示例。
当某些事件发生时,您可以加载 iframe。只要您有一个 HTML 元素准备好接受 iframe。
下面,仅当单击该段落元素时才会出现 iframe。因此从技术上讲,在单击发生之前它将是空白的。
HTML
JavaScript
Raw HTML and JavaScript example.
You could just load in your iframe when some event happens. As long as you have a HTML element ready to accept an iframe.
Below, an iframe will appear only if this paragraph element is clicked. So technically it will be blank until a click happens.
HTML
JavaScript