没有 src 属性的 iframe
我想在页面上创建一个 ,但稍后添加
src
。如果我制作一个没有 src
属性的 iframe,那么它会在某些浏览器中加载当前页面。为 src
设置的正确值是多少,以便它只加载空白 iframe?
我看到的答案是:
about:blank
javascript:false
javascript:void(0)
javascript:"";< /code>
- 空白页面的 url
有明显的赢家吗?如果不是,有哪些权衡?
我不希望在 IE6 及以上的所有浏览器中出现针对 HTTPS url 的混合内容警告,也不出现任何后退按钮、历史记录或重新加载怪异现象。
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?
The answers I've seen are:
about:blank
javascript:false
javascript:void(0)
javascript:"";
- url to a blank page
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不确定是否所有浏览器都支持“about:blank”,所以我就使用您自己的空白页面。
另一个想法:为什么不使用 javascript 添加整个 iframe 而不仅仅是 src?
Not sure if all browsers support "about:blank", so I'd just go with your own blank page then.
Another idea: Why not add the whole iframe using javascript instead of just the src?
创建“空”iframe(例如,作为 iframe shim)时的标准方法是将 src 设置为
javascript:false;
。这是大多数为您创建 iframe shim 的 JavaScript 库所使用的方法(例如 YUI 的 Overlay)。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).怎么样
What about
回复您的评论,澄清您计划使用 iframe 作为表单提交的目标:
我将在服务器上使用一个空文档,发送回
204 no content
。它避免了
javascript:
协议它也是有效的 HTML。
那么如果它产生额外的请求怎么办?正确设置缓存标头,每个客户端只会有一个请求。
Re your comment clarifying that you're planning to use the iframe as the target for a form submission:
I would use an empty document on the server that sends back a
204 no content
.It avoids
javascript:
protocolIt's also valid HTML.
So what if it generates an extra request? Set the caching headers right, and there will be only one request for each client.
javascript:false
:IE10 和 FF(在 Linux 的 v23 中检查)将显示“false”内容。
javascript:void(0)
&&javascript:;
:IE 将在 iframe 中显示“无法显示网页”错误。此外,当将 src 从有效网址设置为
javascript:void(0)
时,页面不会变为空白。关于:空白
:适用于所有浏览器,但 IE 9 向服务器发送路径为“null”的请求。仍然是最好的选择 IMO
结帐 http://jsfiddle.net/eybDj/1
查看 http://jsfiddle.net/sv_in/gRU3V/ 查看 iframe src 在动态更新时如何变化与JS
javascript:false
:IE10 and FF (checked in v23 in Linux) will show 'false' as content.
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.about:blank
:Works in all browsers but IE 9 sends an request to the server with path "null". Still the best bet 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
javascript:false
适用于现代浏览器。我所看到的是,只有当愚蠢的蜘蛛尝试将
javascript:false
作为页面加载时,这才会“失败”。解决方案:阻止愚蠢的蜘蛛。
javascript:false
works in modern browsers.What I've seen is that this only "fails" when dumb spiders try to load
javascript:false
as a page.Solution: Block the dumb spiders.
正如我在此问题中发布的:空的 iframe src 有效吗?,完全省略
src=
属性看起来是可以接受的。As I posted in this question: Is an empty iframe src valid?, it looks acceptable to just leave out the
src=
attribute completely.IMO:如果您不输入 src,您的页面将无法验证。但也差不多了。
如果您输入 src="",您的服务器将记录许多 404 错误。
没有什么比“破坏”真正错误的了。但是,使用 iframe 本身实际上没有错吗?
°-
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.
Nothing is really wrong as in "damaging". But then, is it actually not wrong to use an iframe in itself?
°-
是的,我知道我正在恢复一条旧线程。告我吧我对答案很感兴趣。
我不明白为什么触发器是表单提交会妨碍动态创建 IFrame。这不正是你想要的吗?
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?
遇到了这行代码:
我也 我想删除
javascript:URL
。从 Web 超文本应用程序技术工作组找到此说明 [2019 年 10 月 2 日更新]
[https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element][4.8.5_The_iframe_element]
I run into this line of code:
as well. I wanted to remove
javascript:URL
.Found this note from the Web Hypertext Application Technology Working Group [Updated 2 October 2019]
[https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element][4.8.5_The_iframe_element]