仅通过javascript进行字体检测(无flash)

仅通过javascript进行字体检测(无flash),javascript,Javascript,下面是通过javascript、css方法(无flash)检测浏览器可用的已安装字体的代码。此代码由lalit.org/lab/javascript-css-font-detect修改而来 var Detector = { init: function() { this.h = document.getElementsByTagName("BODY")[0]; this.d = document.createElement("DIV");

下面是通过javascript、css方法(无flash)检测浏览器可用的已安装字体的代码。此代码由lalit.org/lab/javascript-css-font-detect修改而来

 var Detector =
     {
       init: function()
    {
    this.h = document.getElementsByTagName("BODY")[0];
    this.d = document.createElement("DIV");
    this.s = document.createElement("SPAN");
    this.d.appendChild(this.s);
    this.d.style.fontFamily = "sans";
    this.s.style.fontFamily = "sans";
    this.s.style.fontSize = "72px";
    this.s.innerHTML = "mmmmmmmmmmlil";
    this.h.appendChild(this.d);
    this.defaultWidth = this.s.offsetWidth;
    this.defaultHeight = this.s.offsetHeight;
    this.h.removeChild(this.d)
   },
   test: function(a)
   {
    this.h.appendChild(this.d);
    var b = [];
    b.name = this.s.style.fontFamily = a;
    b.width = this.s.offsetWidth;
    b.height = this.s.offsetHeight;
    this.h.removeChild(this.d);
    a = a.toLowerCase();
    if (a == "serif") {
        b.found = true
    } else {
        b.found = (b.width != this.defaultWidth || b.height != this.defaultHeight)
    }
    return b
   },

   getFontList: function()
   {
    this.init();
    var a = ["cursive", "monospace", "serif", "sans-serif", "fantasy", "default",      "Arial", "Arial Black", "Arial Narrow", "Arial Rounded MT Bold", "Book Antiqua", "Bookman Old Style", "Bradley Hand ITC", "Bodoni MT", "Calibri", "Century", "Century Gothic", "Casual", "Comic Sans MS", "Consolas", "Copperplate Gothic Bold", "Courier", "Courier New", "English Text MT", "Felix Titling", "Futura", "Garamond", "Geneva", "Georgia", "Gentium", "Haettenschweiler", "Helvetica", "Impact", "Jokerman", "King", "Kootenay", "Latha", "Liberation Serif", "Lucida Console", "Lalit", "Lucida Grande", "Magneto", "Mistral", "Modena", "Monotype Corsiva", "MV Boli", "OCR A Extended", "Onyx", "Palatino Linotype", "Papyrus", "Parchment", "Pericles", "Playbill", "Segoe Print", "Shruti", "Tahoma", "TeX", "Times", "Times New Roman", "Trebuchet MS", "Verdana", "Verona"];
    var c = "";
    for (i = 0; i < a.length; ++i) {
        var b = this.test(a[i]);
        if (b.found) {
            c += b.name + ","
        }
    }
    return c.slice(0, - 1)
}
  };
var检测器=
{
init:function()
{
this.h=document.getElementsByTagName(“正文”)[0];
this.d=document.createElement(“DIV”);
this.s=document.createElement(“SPAN”);
本.d.儿童(本.s);
this.d.style.fontframy=“sans”;
this.s.style.fontfamine=“sans”;
this.s.style.fontSize=“72px”;
this.s.innerHTML=“mmmmmmlil”;
这个.h.d.孩子(这个.d);
this.defaultWidth=this.s.offsetWidth;
this.defaultHeight=this.s.离视;
this.h.removeChild(this.d)
},
测试:功能(a)
{
这个.h.d.孩子(这个.d);
var b=[];
b、 name=this.s.style.fontFamily=a;
b、 宽度=此s.offsetWidth;
b、 高度=本秒离视线;
这个.h.removeChild(这个.d);
a=a.toLowerCase();
如果(a=“衬线”){
b、 找到=真
}否则{
b、 找到=(b.width!=此.defaultWidth | | b.height!=此.defaultHeight)
}
返回b
},
getFontList:函数()
{
this.init();
var a=[“草书”、“单空格”、“衬线”、“无衬线”、“幻想”、“默认”、“Arial”、“Arial黑”、“Arial窄”、“Arial圆整MT黑体”、“古董书”、“书商旧式”、“布拉德利手笔ITC”、“博多尼MT”、“卡利布里”、“世纪”、“世纪哥特式”、“休闲”、“无MS漫画”、“Consoleas”、“铜版哥特式黑体”、“信使”、“信使新信使”,“英文文本MT”,“菲利克斯标题”,“未来”,“加拉蒙”,“日内瓦”,“乔治亚州”,“Gentium”,“Haettenschweiler”,“Helvetica”,“Impact”,“Jokerman”,“King”,“Kootenay”,“Latha”,“Liberation Serif”,“Lucida Console”,“Lalit”,“Lucida Grande”,“Magnetor”,“Mistral”,“Modena”,“Monotype Corsiva”,“MV Boli”,“OCR A Extended”,“Onyx”,“Palatino Linotype”,“纸莎草”、“羊皮纸”、“伯里克利”、“海报”、“塞戈印花”、“灌木”、“塔荷马”、“特克斯”、“泰晤士报”、“泰晤士报”、“新罗马”、“投石机MS”、“Verdana”、“维罗纳”];
var c=“”;
对于(i=0;i
请帮助我如何使用document.write()来显示字体列表。我只想在浏览器中打印这些字体列表文本。


window.onload=function(){
document.getElementById(“输出”).innerHTML=Detector.getFontList();
}
“我的盒子”上的结果:

草书、单空格、衬线、无衬线、幻想、默认、Arial、Arial黑色、Arial狭窄、古书、书商旧式、卡里布里、世纪、世纪哥特式、漫画无MS、Console、Courier、Courier New、Garamond、Georgia、Helvetica、Impact、Jokerman、Latha、Lucida Console、Magnet、Mistral、单字体Corsiva、Onyx、Palatino Linotype、羊皮纸、海报、Shurti、Tahoma、,威尔达纳投石机


document.write(Detector.getFontList())
@OrangeDog是的。我试过了。但它不起作用。请测试一下,我已经做了。“这很有效。”奥兰吉多格说,但我得到的是一张空白页。你能给我发截图吗?你不能在页面加载后写文档。它会擦干净这一页
<div id="output"></div>

<script>
window.onload=function() {
  document.getElementById("output").innerHTML=Detector.getFontList();
}
</script>