Javascript 没有 src 属性的 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4284605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iframe without an src attribute
提问by Paul Tarjan
I would like to create an <iframe>
on the page, but then add the src
later. If I make an iframe without an src
attribute, then it loads the current page in some browsers. What is the correct value to set for the src
so that it just loads a blank iframe?
我想<iframe>
在页面上创建一个,然后再添加一个src
。如果我创建一个没有src
属性的 iframe ,那么它会在某些浏览器中加载当前页面。为 the 设置的正确值是多少,src
以便它只加载一个空白的 iframe?
The answers I've seen are:
我看到的答案是:
about:blank
javascript:false
javascript:void(0)
javascript:"";
- url to a blank page
about:blank
javascript:false
javascript:void(0)
javascript:"";
- url到空白页
Is there a clear winner? If not, what are the tradeoffs?
有明确的赢家吗?如果不是,有什么权衡?
I'd like to not have mixed content warnings for HTTPS urls, nor any back-button, history, or reload weirdness in all browsers from IE6 onward.
我不希望 HTTPS url 的混合内容警告,也没有任何后退按钮、历史记录或从 IE6 开始的所有浏览器中的重新加载怪异。
采纳答案by nefarioustim
Standard approach when creating an "empty" iframe (as an iframe shim, for example), is to set the src as javascript:false;
. This is the method used by most of the JavaScript libraries that create iframe shims for you (e.g. YUI's Overlay).
创建“空”iframe(例如作为 iframe 垫片)时的标准方法是将 src 设置为javascript:false;
. 大多数为您创建 iframe 垫片的 JavaScript 库都使用这种方法(例如 YUI 的 Overlay)。
回答by Mario
Not sure if all browsers support "about:blank", so I'd just go with your own blank page then.
不确定是否所有浏览器都支持“about:blank”,所以我会选择你自己的空白页面。
Another idea: Why not add the whole iframe using javascript instead of just the src?
另一个想法:为什么不使用 javascript 而不仅仅是 src 添加整个 iframe?
回答by Pekka
Re your comment clarifying that you're planning to use the iframe as the target for a form submission:
重新评论澄清您计划使用 iframe 作为表单提交的目标:
I would use an empty document on the server that sends back a 204 no content
.
我会在服务器上使用一个空文档,它会发回204 no content
.
It avoids
它避免了
- "mixed content" warnings in IE and HTTPS mode
- Unnecessary errors because a client doesn't understand the
javascript:
protocol - and other exotic shenanigans.
- IE 和 HTTPS 模式下的“混合内容”警告
- 由于客户端不理解
javascript:
协议而导致的不必要的错误 - 和其他异国情调的恶作剧。
It's also valid HTML.
它也是有效的 HTML。
So what if it generates an extra request? Set the caching headers right, and there will be only one request for each client.
那么如果它产生一个额外的请求呢?正确设置缓存头,每个客户端只有一个请求。
回答by Jan Sverre
What about
关于什么
about:blank
回答by sv_in
javascript:false
:
IE10 and FF (checked in v23 in Linux) will show 'false' as content.
javascript:false
:
IE10 和 FF(在 Linux 中的 v23 中检查)将显示“false”作为内容。
javascript:void(0)
&& javascript:;
:
IE will show 'cannot display the webpage' error in the iframe. Also, when setting the src from a valid url to javascript:void(0)
, the page will not get blank.
javascript:void(0)
&& javascript:;
:
IE 将在 iframe 中显示“无法显示网页”错误。此外,当将 src 从有效 url 设置为 时javascript:void(0)
,页面不会变为空白。
about:blank
:
Works in all browsers but IE 9 sends an request to the server with path "null". Still the best bet IMO
about:blank
:
适用于所有浏览器,但 IE 9 向服务器发送路径为“null”的请求。仍然是 IMO 的最佳选择
Checkout http://jsfiddle.net/eybDj/1
Checkout http://jsfiddle.net/sv_in/gRU3V/to see how iframe src changes on dynamic updation with JS
结帐http://jsfiddle.net/eybDj/1
结帐http://jsfiddle.net/sv_in/gRU3V/以查看 iframe src 在使用 JS 动态更新时如何变化
回答by Steve Magruder
javascript:false
works in modern browsers.
javascript:false
适用于现代浏览器。
What I've seen is that this only "fails" when dumb spiders try to load javascript:false
as a page.
我所看到的是,当愚蠢的蜘蛛尝试加载javascript:false
为页面时,这只会“失败” 。
Solution: Block the dumb spiders.
解决方案:阻止愚蠢的蜘蛛。
回答by rjmunro
As I posted in this question: Is an empty iframe src valid?, it looks acceptable to just leave out the src=
attribute completely.
正如我在这个问题中发布的:Is an empty iframe src valid? ,src=
完全省略该属性看起来是可以接受的。
回答by J C
Yes, I know I'm reviving an old thread. Sue me. I'm interested in the answer.
是的,我知道我正在恢复一个旧线程。告我。我对答案很感兴趣。
I don't understand why having the trigger being a form submit precludes dynamically creating the IFrame. Does this not do exactly what you want?
我不明白为什么将触发器作为表单提交会阻止动态创建 IFrame。这不正是你想要的吗?
<html>
<head>
<script type="text/javascript">
function setIFrame(elemName, target, width, height) {
document.getElementById(elemName).innerHTML="<iframe width="+width+" height="+height+" src='"+target+"'></iframe>";
}
</script>
</head>
<body>
<div id="iframe" style="width:400px; height:200px"></div>
<form onSubmit="setIFrame('iframe', 'http://www.google.com', 400, 200); return false;">
<input type="submit" value="Set IFrame"/>
</form>
</body>
</html>
回答by pixeline
IMO: if you don't put the src, your page won't validate. But's about it. If you put a src="", your server will log many 404 errors.
IMO:如果您不放置 src,您的页面将无法验证。但就是这样。如果您输入 src="",您的服务器将记录许多 404 错误。
Nothing is really wrong as in "damaging". But then, is it actually not wrong to use an iframe in itself?
没有什么是真正错误的“破坏”。但是,本身使用 iframe 真的没有错吗?
°-
°-
回答by Alberto Carreras
I run into this line of code:
我遇到了这行代码:
iframe.setAttribute("src", "javascript:false");
as well. I wanted to remove javascript:URL
.
以及。我想删除javascript:URL
.
Found this note from the Web Hypertext Application Technology Working Group [Updated 2 October 2019]
从 Web 超文本应用技术工作组中找到此说明 [2019 年 10 月 2 日更新]
The otherwise steps for iframe or frame elements are as follows:
If the element has no src attribute specified, or its value is the empty string, let url be the URL "about:blank".
iframe 或 frame 元素的其他步骤如下:
如果元素没有指定 src 属性,或者它的值为空字符串,则让 url 为 URL“about:blank”。