javascriptappendchildself-executing-functioncreatetextnode

Trouble Creating Text Node in Head via JS Self-Executing Function


This is the first time that I am working with self-executing anonymous functions, however, I believe that I am missing something that is both fundamental and right in front of my face. Essentially, I am attempting to pass an argument through the function 'writeFontFace()' and have it write the following code inside the head of my document, like so:

@font-face {
    font-family: 'arrows';
    src: url('fonts/arrows/arrows.eot?');
    src: url('fonts/arrows/arrows.eot?#iefix') format('embedded-opentype'),
         url('fonts/arrows/arrows.woff?') format('woff'),
         url('fonts/arrows/arrows.ttf?') format('truetype'),
         url('fonts/arrows/arrows.svg?#arrows') format('svg');
    font-weight: normal;
    font-style: normal;
}

Now, if I un-comment the 'return' statement at the end of this function (writeFontFace) I do receive the appropriate output that I am expecting to receive, however (for whatever reason), it is not appending the 'script' variable (containing the aforementioned @font-face definition) inside of the head of my document? Can't quite figure out why. Any suggestions and/or comments would surely be appreciated... And again, this is my first time working with self-executing anonymous functions, therefore, I would appreciate a low level of sarcasm if anyone feels as though they might have some constructive criticism and/or advice. As always, much appreciation in advance.

~ Cheers


Solution

  • Essentially, if anyone else runs into a similar problem, the code below should supply you with the appropriate fix:

    (function() {
    
         /**
          * bpIconFont               {Constructor}               Core: constructor for this library; ensures that this library is instantiated if an internal method is called
          */
         var bpIconFont = function() {
             if(!(this instanceof bpIconFont))
             return new bpIconFont();
         };
    
         /**
          * bpIconFont.fn
          */
         bpIconFont.fn = bpIconFont.prototype = {
             init: function() {
                 console.log('bpIconFont Initialized!');
             }
         }
    
         window.bpIconFont = bpIconFont;                         // Expose: anonymous self-executing function to DOM
    
    })();
    
         /**
          * Object Literal           {Property Values}           Constant: property values to be used throughout this object
          */
        vars = {
            decCharPrefix : '&#',
            decCharSuffix : ';',
            baseGlyphValue: 59392,
        };
    
    /**
     * getFontDirectroy         {Method}                    Gets: generates the directory to which the passed font will be placed, via relative path
     * @param                   {Array/String} font         Converts: the passed array or string and generates a 'relative path' for the desired font
     * @return                  {Array/String}              Returns: the relative path for the font
     */
    bpIconFont.fn.getFontDirectroy = function(font) {
        var fontDir = 'fonts/' + font + '/';
        return fontDir;
    };
    
    /**
     * getFontDirectroy         {Method}                    Gets: generates the directory to which the passed font will be placed, via relative path
     * @param                   {Array/String} font         Converts: the passed array or string and generates a 'relative path' for the desired font
     * @return                  {Array/String}              Returns: the relative path for the font
     */
    bpIconFont.fn.writeFontFace = function(font) {
        var dir    = bpIconFont().getFontDirectroy(font);
        var head   = document.getElementsByTagName("head")[0];
        var script = document.createElement('style');
            script.setAttribute('type', 'text/css');
    
        var cssDef = '\ndiv#output-window {' +
                     '\n\tfont-family:\'' + font + '\';' +
                     '\n}';
    
        var fontFace = '@font-face {' +
                      '\n\tfont-family: \'' + font +
                    '\';\n\tsrc: url(\'' + dir + font + '.eot?\');' +
                       '\n\tsrc: url(\'' + dir + font + '.eot?#iefix\') format(\'embedded-opentype\'),' +
                         '\n\t\t url(\'' + dir + font + '.woff?\') format(\'woff\'),' +
                         '\n\t\t url(\'' + dir + font + '.ttf?\') format(\'truetype\'),' +
                         '\n\t\t url(\'' + dir + font + '.svg?#' + font + '\') format(\'svg\');' +
                       '\n\tfont-weight: normal;' +
                       '\n\tfont-style: normal;' +
                    '\n}';
    
        script.appendChild(document.createTextNode(fontFace));
        script.appendChild(document.createTextNode(cssDef));
        head.appendChild(script);
    
    };