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..
我想要一个iframe最初的src是空的,然后一旦页面加载;调用JS函数,然后将src值设置为实际值。
So is <iframe src="#" />
valid OR do I need to use something else like javascript:;
, etc
那么是否有效,或者我是否需要使用javascript:;等等</p>
9 个解决方案
#1
76
just <iframe src='about:blank'></iframe>
只是< iframe的src = ':空白' > < / iframe >
#2
44
The HTML 5 working draft, section 4.8.2, says (emphasis mine):
HTML 5的工作草案,第4.8.2节说(强调我的):
The
src
attribute gives the address of a page that the nested browsing context is to contain. The attribute, if present, must be a valid non-empty URL potentially surrounded by spaces.src属性给出嵌套浏览上下文要包含的页面的地址。属性,如果存在,必须是一个有效的非空URL,可能被空间包围。
According to RFC 3986, valid URLs must begin with a scheme name, and relative references cannot only consist in a fragment.
根据RFC 3986,有效的url必须以计划名开头,相对引用不能仅包含在片段中。
Therefore, #
is not a valid URL, and should not be used as the value of the src
attribute.
因此,#不是一个有效的URL,不应该用作src属性的值。
Use about:blank instead.
使用:空白。
#3
21
No, it is not valid to specify an empty iframe src.
不,指定一个空的iframe src是无效的。
You should use <iframe src="about:blank" />
.
您应该使用。</p>
#
is ment 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.
#是对当前页面中的锚点的引用(或者,在处理AJAX请求时经常用作路由方案)。使用它作为iframe的源将是无意义的,因为iframe不引用当前页面上的内容,也不使用AJAX请求。
about:blank
is a cross-browser standard to display a blank document.
about:blank是一个跨浏览器的标准,用于显示空白文档。
Update June 8th 2012:
2012年6月8日更新:
It seems that the 'living' spec no longer renders an iframe invalid if the src
attribute is missing:
如果src属性缺失,“living”规范似乎不再呈现iframe无效:
If, when the element is created, the srcdoc attribute is not set, and the src attribute is either also not set or set but its value cannot be resolved, the browsing context will remain at the initial about:blank page.
如果在创建元素时,没有设置srcdoc属性,而且src属性也没有设置或设置,但其值无法解析,那么浏览上下文将保持在最初的about:blank page。
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 the about:blank
url.
但是,如果没有设置这两个属性,浏览上下文将默认为about:blank。为了提供适当的向后兼容性,建议使用冗长的url,并暂时提供about:blank url。
#4
10
It looks like you can also leave out the src completely:
看起来你也可以完全忽略src:
http://dev.w3.org/html5/spec/Overview.html#the-iframe-element
http://dev.w3.org/html5/spec/Overview.html the-iframe-element
If, when the element is created, the srcdoc attribute is not set, and the src attribute is either also not set or set but its value cannot be resolved, the browsing context will remain at the initial about:blank page.
如果在创建元素时,没有设置srcdoc属性,而且src属性也没有设置或设置,但其值无法解析,那么浏览上下文将保持在最初的about:blank page。
#5
1
You can use about:blank
in the src
attribute (as mentioned by ariel earlier), otherwise it would throw an error when serving from a secure page.
您可以使用about: src属性中的空白(如ariel前面提到的),否则它将在从安全页面中服务时抛出错误。
A secure page https
would throw an error of possibly un-secure data on the secure website.
安全页面https将在安全网站上抛出可能不安全的数据错误。
#6
1
You could try adding the iframe through Javascript, so you wouldn't need to have a blank one in the HTML:
你可以尝试通过Javascript添加iframe,这样你就不需要在HTML中有一个空的:
(jQuery example)
(jQuery示例)
<script type="text/javascript">
$().ready(function() {
$("<iframe />").attr("src", "http://www.bbc.co.uk/").appendTo("body");
});
</script>
Adding the iframe with Javascript allows graceful degradation - users without Javascript won't see a blank iframe.
使用Javascript添加iframe可以进行优雅的降级——没有Javascript的用户不会看到空白iframe。
#7
1
As part of the about URI scheme standard, about:blank is probably the best option as it has very good browser support.
作为about URI方案标准的一部分,about:blank可能是最好的选择,因为它有很好的浏览器支持。
about:blank Returns a blank HTML document with the media type text/html and character encoding UTF-8. This is widely used to load blank pages into browsing contexts, such as iframes within HTML, which may then be modified by scripts. You can see further here
about:blank返回一个带有媒体类型文本/ HTML和字符编码UTF-8的空白HTML文档。这被广泛用于将空白页面加载到浏览上下文中,例如HTML中的iframe,然后可以通过脚本对其进行修改。你可以在这里看得更远
#8
0
For the record
备案
Let's say the next example (Firefox 58 but may be its present in all browsers).
我们来看下一个例子(Firefox 58,但可能在所有浏览器中都有)。
<link href="css.css" rel="stylesheet" type="text/css"/>
<iframe src='about:blank'></iframe>
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.
Iframe在css之前加载,所以在加载css之前,页面的呈现是可见的。例如,在一秒钟内,页面看起来没有css。
Instead:
而不是:
<link href="css.css" rel="stylesheet" type="text/css"/>
<iframe src='blank.html'></iframe>
It works fine, the css is loaded and the iframe is loaded finally.
它工作得很好,css被加载,iframe最终被加载。
#9
0
If you don't want to use about:blank
you may use javascript as an src
for your iframe
like the following example:
如果您不想使用about:blank,您可以使用javascript作为您的iframe的src,例如以下示例:
<iframe name="TV" id="tv" style="width:100%; background: #800000" src="javascript:document.write('<h3>Results Window</h3>')"></iframe>
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 the iframe
name
attribute i.e in that example TV
.
上面的示例将初始化一个具有maroon背景的iframe,它具有一个简单的消息
结果窗口
。但是,您的链接的目标应该等于iframe name属性i。e在那个例子里是电视。#1
76
just <iframe src='about:blank'></iframe>
只是< iframe的src = ':空白' > < / iframe >
#2
44
The HTML 5 working draft, section 4.8.2, says (emphasis mine):
HTML 5的工作草案,第4.8.2节说(强调我的):
The
src
attribute gives the address of a page that the nested browsing context is to contain. The attribute, if present, must be a valid non-empty URL potentially surrounded by spaces.src属性给出嵌套浏览上下文要包含的页面的地址。属性,如果存在,必须是一个有效的非空URL,可能被空间包围。
According to RFC 3986, valid URLs must begin with a scheme name, and relative references cannot only consist in a fragment.
根据RFC 3986,有效的url必须以计划名开头,相对引用不能仅包含在片段中。
Therefore, #
is not a valid URL, and should not be used as the value of the src
attribute.
因此,#不是一个有效的URL,不应该用作src属性的值。
Use about:blank instead.
使用:空白。
#3
21
No, it is not valid to specify an empty iframe src.
不,指定一个空的iframe src是无效的。
You should use <iframe src="about:blank" />
.
您应该使用。</p>
#
is ment 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.
#是对当前页面中的锚点的引用(或者,在处理AJAX请求时经常用作路由方案)。使用它作为iframe的源将是无意义的,因为iframe不引用当前页面上的内容,也不使用AJAX请求。
about:blank
is a cross-browser standard to display a blank document.
about:blank是一个跨浏览器的标准,用于显示空白文档。
Update June 8th 2012:
2012年6月8日更新:
It seems that the 'living' spec no longer renders an iframe invalid if the src
attribute is missing:
如果src属性缺失,“living”规范似乎不再呈现iframe无效:
If, when the element is created, the srcdoc attribute is not set, and the src attribute is either also not set or set but its value cannot be resolved, the browsing context will remain at the initial about:blank page.
如果在创建元素时,没有设置srcdoc属性,而且src属性也没有设置或设置,但其值无法解析,那么浏览上下文将保持在最初的about:blank page。
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 the about:blank
url.
但是,如果没有设置这两个属性,浏览上下文将默认为about:blank。为了提供适当的向后兼容性,建议使用冗长的url,并暂时提供about:blank url。
#4
10
It looks like you can also leave out the src completely:
看起来你也可以完全忽略src:
http://dev.w3.org/html5/spec/Overview.html#the-iframe-element
http://dev.w3.org/html5/spec/Overview.html the-iframe-element
If, when the element is created, the srcdoc attribute is not set, and the src attribute is either also not set or set but its value cannot be resolved, the browsing context will remain at the initial about:blank page.
如果在创建元素时,没有设置srcdoc属性,而且src属性也没有设置或设置,但其值无法解析,那么浏览上下文将保持在最初的about:blank page。
#5
1
You can use about:blank
in the src
attribute (as mentioned by ariel earlier), otherwise it would throw an error when serving from a secure page.
您可以使用about: src属性中的空白(如ariel前面提到的),否则它将在从安全页面中服务时抛出错误。
A secure page https
would throw an error of possibly un-secure data on the secure website.
安全页面https将在安全网站上抛出可能不安全的数据错误。
#6
1
You could try adding the iframe through Javascript, so you wouldn't need to have a blank one in the HTML:
你可以尝试通过Javascript添加iframe,这样你就不需要在HTML中有一个空的:
(jQuery example)
(jQuery示例)
<script type="text/javascript">
$().ready(function() {
$("<iframe />").attr("src", "http://www.bbc.co.uk/").appendTo("body");
});
</script>
Adding the iframe with Javascript allows graceful degradation - users without Javascript won't see a blank iframe.
使用Javascript添加iframe可以进行优雅的降级——没有Javascript的用户不会看到空白iframe。
#7
1
As part of the about URI scheme standard, about:blank is probably the best option as it has very good browser support.
作为about URI方案标准的一部分,about:blank可能是最好的选择,因为它有很好的浏览器支持。
about:blank Returns a blank HTML document with the media type text/html and character encoding UTF-8. This is widely used to load blank pages into browsing contexts, such as iframes within HTML, which may then be modified by scripts. You can see further here
about:blank返回一个带有媒体类型文本/ HTML和字符编码UTF-8的空白HTML文档。这被广泛用于将空白页面加载到浏览上下文中,例如HTML中的iframe,然后可以通过脚本对其进行修改。你可以在这里看得更远
#8
0
For the record
备案
Let's say the next example (Firefox 58 but may be its present in all browsers).
我们来看下一个例子(Firefox 58,但可能在所有浏览器中都有)。
<link href="css.css" rel="stylesheet" type="text/css"/>
<iframe src='about:blank'></iframe>
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.
Iframe在css之前加载,所以在加载css之前,页面的呈现是可见的。例如,在一秒钟内,页面看起来没有css。
Instead:
而不是:
<link href="css.css" rel="stylesheet" type="text/css"/>
<iframe src='blank.html'></iframe>
It works fine, the css is loaded and the iframe is loaded finally.
它工作得很好,css被加载,iframe最终被加载。
#9
0
If you don't want to use about:blank
you may use javascript as an src
for your iframe
like the following example:
如果您不想使用about:blank,您可以使用javascript作为您的iframe的src,例如以下示例:
<iframe name="TV" id="tv" style="width:100%; background: #800000" src="javascript:document.write('<h3>Results Window</h3>')"></iframe>
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 the iframe
name
attribute i.e in that example TV
.
上面的示例将初始化一个具有maroon背景的iframe,它具有一个简单的消息