对@yc的答案的一个小改进是从通常无论如何都会使用和缓存的JavaScript文件中注入Base64编码的favicon,并且还通过在相关标记中为其提供数据URI来抑制浏览器请求的标准行为。favicon.ico
meta
这种技术避免了额外的http请求,并被确认可以在Windows 7上的最新版本的Chrome,Firefox和Opera中工作。但是,它似乎至少在Internet Explorer 9中不起作用。
文件索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Suppress browser request for favicon.ico -->
<link rel="shortcut icon"type="image/x-icon" href="data:image/x-icon;,">
<script src="script.js"></script>
...
文件脚本.js
var favIcon = "\
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABrUlEQVR42mNkwAOepOgxMTD9mwhk\
[...truncated for brevity...]
IALgNIBUQBUDAFi2whGNUZ3eAAAAAElFTkSuQmCC";
var docHead = document.getElementsByTagName('head')[0];
var newLink = document.createElement('link');
newLink.rel = 'shortcut icon';
newLink.href = 'data:image/png;base64,'+favIcon;
docHead.appendChild(newLink);
/* Other JavaScript code would normally be in here too. */