Skip to main content

Getting System Fonts Using JavaScript

By Sam Deering

JavaScript

Share:

Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

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('');
    $('#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('').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

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Get practical advice to start your career in programming!


Master complex transitions, transformations and animations in CSS!