我只是想解释更多以前的马克的答案(有些人不明白,例如user7892745)。
1)当你启动你的网页或网页应用程序时,它会检查一个不寻常的字体(如中文Konfuciuz字体http://www.fontspace.com/apostrophic-lab/konfuciuz)。
下面是样本网页与功能的代码检查字体(称为isFontAvailable):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
/**
* Checks if a font is available to be used on a web page.
*
* @param {String} fontName The name of the font to check
* @return {Boolean}
* @license MIT
* @copyright Sam Clarke 2013
* @author Sam Clarke <[email protected]>
*/
(function (document) {
var width;
var body = document.body;
var container = document.createElement('span');
container.innerHTML = Array(100).join('wi');
container.style.cssText = [
'position:absolute',
'width:auto',
'font-size:128px',
'left:-99999px'
].join(' !important;');
var getWidth = function (fontFamily) {
container.style.fontFamily = fontFamily;
body.appendChild(container);
width = container.clientWidth;
body.removeChild(container);
return width;
};
// Pre compute the widths of monospace, serif & sans-serif
// to improve performance.
var monoWidth = getWidth('monospace');
var serifWidth = getWidth('serif');
var sansWidth = getWidth('sans-serif');
window.isFontAvailable = function (font) {
return monoWidth !== getWidth(font + ',monospace') ||
sansWidth !== getWidth(font + ',sans-serif') ||
serifWidth !== getWidth(font + ',serif');
};
})(document);
function isProtocolAvailable()
{
if (isFontAvailable('Konfuciuz'))
{
return true;
}
else
{
return false;
}
}
function checkProtocolAvail()
{
if (isProtocolAvailable())
{
alert('Custom protocol is available!');
}
else
{
alert('Please run executable to install protocol!');
}
}
</script>
<h3>Check if custom protocol was installed or not</h3>
<pre>
<input type="button" value="Check if custom protocol was installed!" onclick="checkProtocolAvail()">
</body>
</html>
2)当用户打开该网页第一次,字体将不会被安装,所以他会得到一条消息说“请运行可执行文件以安装自定义协议...”。
3)他将运行将安装字体的可执行文件。你的EXE可以直接将字体文件复制(在我的情况下,它是KONFUC __ TTF)到C:\ Windows目录或使用这样的代码(Delphi的例子):
// Adding the font ..
AddFontResource(PChar('XXXFont.TTF'));
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
4)之后,当用户再次运行Web应用程序,他得到“自定义协议可用!”消息,因为这次是安装字体。
在Google Chrome,Internet Explorer和Firefox上测试 - 效果很好!
您可能想要阅读http://stackoverflow.com/questions/836777/how-to-detect-browsers-protocol-handlers – 2010-05-20 08:39:42
Thx,已经尝试过大部分描述的方法。似乎没有好的方法可以在所有流行的浏览器中实现此功能,而无需警报或其他问题。 – 2010-05-20 08:50:07