Click here to Skip to main content
15,884,657 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I made an select menu that works with the google-font-Api from Font-Api. I made to function in JSBIN here is my work http://jsbin.com/ocutuk/18/

But when i made the copy of my code in a html page its not even loading the font names in page. i tried to make it work but still it is in dead end. This is my html code
XML
<html>
     <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
      <meta charset=utf-8 />
      <title>FONT API</title>
      <script>
        function SetFonts(fonts) { 
        for (var i = 0; i < fonts.items.length; i++) {      
        $('#styleFont')
         .append($("<option></option>")
         .attr("value", fonts.items[i].family)
         .text(fonts.items[i].family));
        }    
      }

      var script = document.createElement('script');
      script.src = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyB8Ua6XIfe-gqbkE8P3XL4spd0x8Ft7eWo&callback=SetFonts';
      document.body.appendChild(script);

      WebFontConfig = {
    google: { families: ['ABeeZee', 'Abel', 'Abril Fatface', 'Aclonica', 'Acme', 'Actor', 'Adamina', 'Advent Pro', 'Aguafina Script', 'Akronim', 'Aladin', 'Aldrich', 'Alegreya', 'Alegreya SC', 'Alex Brush', 'Alfa Slab One', 'Alice', 'Alike', 'Alike Angular', 'Allan', 'Allerta', 'Allerta Stencil', 'Allura', 'Almendra', 'Almendra Display', 'Almendra SC', 'Amarante', 'Amaranth', 'Amatic SC', 'Amethysta', 'Anaheim', 'Andada', 'Andika']}
    };
    (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
    })();

     $("#styleFont").change(function (){
       var id =$('#styleFont option' +':selected').val();  
                                $("#custom_text").css('font-family',id);   
       });

      </script>
      
          #custom_text {
	  font-family: Arial;
	  resize: none;
	  margin-top: 20px;
	  width: 500px;
         }
          #styleFont {
	  width: 100px;
         }
        
        </head>
        <body>
        <select id="styleFont">
        </select>
        <textarea id="custom_text"></textarea>
        </body>
        </html>


How can i make it work. Whats the mistake i am making here.
and i tried to make all fonts to work in select menu it only load 165 fonts. How can i add it
Posted
v3

1 solution

test this
JavaScript
window.onload = function(){

 function SetFonts(fonts) { 
        for (var i = 0; i < fonts.items.length; i++) {      
        $('#styleFont')
         .append($("<option></option>")
         .attr("value", fonts.items[i].family)
         .text(fonts.items[i].family));
        }    
      }
 
      var script = document.createElement('script');
      script.src = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyB8Ua6XIfe-gqbkE8P3XL4spd0x8Ft7eWo&callback=SetFonts';
      document.body.appendChild(script);
 
      WebFontConfig = {
    google: { families: ['ABeeZee', 'Abel', 'Abril Fatface', 'Aclonica', 'Acme', 'Actor', 'Adamina', 'Advent Pro', 'Aguafina Script', 'Akronim', 'Aladin', 'Aldrich', 'Alegreya', 'Alegreya SC', 'Alex Brush', 'Alfa Slab One', 'Alice', 'Alike', 'Alike Angular', 'Allan', 'Allerta', 'Allerta Stencil', 'Allura', 'Almendra', 'Almendra Display', 'Almendra SC', 'Amarante', 'Amaranth', 'Amatic SC', 'Amethysta', 'Anaheim', 'Andada', 'Andika']}
    };
    (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
    })();
 
     $("#styleFont").change(function (){
       var id =$('#styleFont option' +':selected').val();  
                                $("#custom_text").css('font-family',id);   
       });

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900