包含没有src的图像的有效方法是什么? [英] What's the valid way to include an image with no src?

查看:84
本文介绍了包含没有src的图像的有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,我将在稍后用javascript动态填充src,但为了方便起见,我希望图片标签存在于页面加载中,但不显示任何内容。我知道< img src =''/> 是无效的,所以最好的方法是什么?

解决方案

虽然没有有效的方法可以省略图片的来源,但是来源,不会导致服务器点击。我最近有一个与 iframe s类似的问题,并确定 //:0 是最佳选择。不,真的!

// (忽略协议)开始会导致当前页面的协议为使用,防止HTTPS页面中的不安全内容警告。跳过主机名不是必需的,但使其缩短。最后,一个:0 端口确保服务器请求不能被创建(根据规范,它不是有效的端口)。



这是我发现的唯一URL,它在任何浏览器中都没有导致服务器点击或错误消息。如果在通过HTTPS提供的页面上使用常规选项 - javascript:void(0),则会导致IE7中出现不安全内容警告。任何其他端口都会导致尝试连接服务器,即使是无效地址。 (有些浏览器会发出无效请求并等待它们超时。)



这已在Chrome,Safari 5,FF 3.6和IE 6/7 / 8,但我希望它可以在任何浏览器中工作,因为它应该是杀死任何尝试请求的网络层。


I have an image that I will dynamically populate with a src later with javascript but for ease I want the image tag to exist at pageload but just not display anything. I know <img src='' /> is invalid so what's the best way to do this?

解决方案

While there is no valid way to omit an image's source, there are sources which won't cause server hits. I recently had a similar issue with iframes and determined //:0 to be the best option. No, really!

Starting with // (omitting the protocol) causes the protocol of the current page to be used, preventing "insecure content" warnings in HTTPS pages. Skipping the host name isn't necessary, but makes it shorter. Finally, a port of :0 ensures that a server request can't be made (it isn't a valid port, according to the spec).

This is the only URL which I found caused no server hits or error messages in any browser. The usual choice — javascript:void(0) — will cause an "insecure content" warning in IE7 if used on a page served via HTTPS. Any other port caused an attempted server connection, even for invalid addresses. (Some browsers would simply make the invalid request and wait for them to time out.)

This was tested in Chrome, Safari 5, FF 3.6, and IE 6/7/8, but I would expect it to work in any browser, as it should be the network layer which kills any attempted request.

这篇关于包含没有src的图像的有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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