Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML API
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
<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 9-Dec-12 20:58pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

test this
 
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);   
       });
 
}
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100