Getting System Fonts Using JavaScript

Sam Deering

Just a couple of ways to get the System Fonts using JavaScript. There are ref links below with demos.

Getting System Fonts Using JavaScript

try {
    //adds a swf to the page and reads fonts
    $('body').append('<div id="flashcontent"></div>');
    $('#flashcontent').flash(
    {
        "src": "resources/FontList.swf",
        "width": "1",
        "height": "1",
        "swliveconnect": "true",
        "id": "flashfontshelper",
        "name": "flashfontshelper"
    },
    { update: false });

    //timeout required for swf to settle.
    setTimeout(function()
    {
        console.log(window.fonts);
        return window.fonts;
    }, _this.settings.flashDelay);
}
catch(ex) {
    _this.errors.push('No fonts detected.');
    return fonts;
}

Source Demo

Another way

try {
    //adds a swf to the page and reads fonts
    $('body').append('<div id="flashcontent"></div>').flash(
    {
        "src": "resources/fonts.swf",
        "width": "1",
        "height": "1",
        "swliveconnect": "true",
        "id": "flashfontshelper",
        "name": "flashfontshelper"
    },
    { update: false });

    //timeout required for swf to settle.
    setTimeout(function()
    {
        var fonts = "",
            obj = document.getElementById("flashfontshelper");

        //get fonts
        if (obj && typeof(obj.GetVariable) != "undefined") {
          fonts = obj.GetVariable("/:user_fonts").replace(/,/g,", ");
        }
        if (!fonts) {
          fonts = "No Flash or Java fonts detected";
          _this.errors.push('No Flash or Java fonts detected.');
        }
        console.log(fonts);
        return fonts;
    }, 100);
    return true;
}
catch(ex) {
    fonts = this.errors.push('No Flash or Java fonts detected.');
}

errors:
Uncaught TypeError: Property ‘fontList’ of object [object Object] is not a function

Demo

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.