如何防止favicon.ico请求? [英] How to prevent favicon.ico requests?

查看:184
本文介绍了如何防止favicon.ico请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有favicon.ico,但是我的浏览器总是向其发出请求.

I don't have a favicon.ico, but my browser always makes a request for it.

是否可以阻止浏览器从我的网站请求收藏夹图标?也许在HTML标头中有一些META-TAG?

Is it possible to prevent the browser from making a request for the favicon from my site? Maybe some META-TAG in the HTML header?

推荐答案

我首先要说的是,在网页上放置收藏夹图标是一件好事(通常).

I will first say that having a favicon in a Web page is a good thing (normally).

但是,并非总是如此,开发人员有时需要一种避免额外有效负载的方法.例如,IFRAME会在不显示图标的情况下请求其图标. 最糟糕的是,在Chrome和Android中,IFRAME会生成3个网站图标请求:

However it is not always desired and sometime developers need a way to avoid the extra payload. For example an IFRAME would request a favicon without showing it. Worst yet, in Chrome and Android an IFRAME will generate 3 requests for favicons:

"GET /favicon.ico HTTP/1.1" 404 183
"GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 197
"GET /apple-touch-icon.png HTTP/1.1" 404 189

以下使用数据URI,可用于避免伪造的favicon请求:

The following uses data URI and can be used to avoid fake favicon requests:

<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"> 

有关参考,请参见此处:

For references see here:

  • https://github.com/h5bp/html5-boilerplate/issues/1103
  • https://twitter.com/diegoperini/status/4882543836930048

更新1:

从注释(jpic)看来,Firefox> = 25不再喜欢上述语法.我在Firefox 27上进行了测试,但是在Webkit/Chrome上仍然可以正常使用.

From the comments (jpic) it looks like Firefox >= 25 doesn't like the above syntax anymore. I tested on Firefox 27 and it doesn't work while it still work on Webkit/Chrome.

因此,这里是新版本,应涵盖所有最新的浏览器.我测试了Safari,Chrome和Firefox:

So here is the new one that should cover all recent browsers. I tested Safari, Chrome and Firefox:

<link rel="icon" href="data:;base64,=">

我忽略了快捷方式" "rel"名称中的名称属性值,因为这仅适用于较旧的IE和IE< 8也不喜欢dataURIs.尚未在IE8上进行测试.

I left out the "shortcut" name from the "rel" attribute value since that's only for older IE and versions of IE < 8 doesn't like dataURIs either. Not tested on IE8.

更新2:

如果您需要文档来针对HTML5进行验证,请改用以下方法:

If you need your document to validate against HTML5 use this instead:

<link rel="icon" href="data:;base64,iVBORw0KGgo=">

这篇关于如何防止favicon.ico请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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