Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

font detection through javascript

Posted on 2013-06-23
10
373 Views
Last Modified: 2013-06-25
here is the code for detecting installed fonts available to a browser though javascript,css method.(no flash)
var Detector =
{
    init: function()
    {
        this.h = document.getElementsByTagName("BODY")[0];
        this.d = document.createElement("DIV");
        this.s = document.createElement("SPAN");
        this.d.appendChild(this.s);
        this.d.style.fontFamily = "sans";
        this.s.style.fontFamily = "sans";
        this.s.style.fontSize = "72px";
        this.s.innerHTML = "mmmmmmmmmmlil";
        this.h.appendChild(this.d);
        this.defaultWidth = this.s.offsetWidth;
        this.defaultHeight = this.s.offsetHeight;
        this.h.removeChild(this.d)
    },
    test: function(a)
    {
        this.h.appendChild(this.d);
        var b = [];
        b.name = this.s.style.fontFamily = a;
        b.width = this.s.offsetWidth;
        b.height = this.s.offsetHeight;
        this.h.removeChild(this.d);
        a = a.toLowerCase();
        if (a == "serif") {
            b.found = true
        } else {
            b.found = (b.width != this.defaultWidth || b.height != this.defaultHeight)
        }
        return b
    },
    getFontList: function()
    {
        this.init();
        var a = ["cursive", "monospace", "serif", "sans-serif", "fantasy", "default", "Arial", "Arial Black", "Arial Narrow", "Arial Rounded MT Bold", "Book Antiqua", "Bookman Old Style", "Bradley Hand ITC", "Bodoni MT", "Calibri", "Century", "Century Gothic", "Casual", "Comic Sans MS", "Consolas", "Copperplate Gothic Bold", "Courier", "Courier New", "English Text MT", "Felix Titling", "Futura", "Garamond", "Geneva", "Georgia", "Gentium", "Haettenschweiler", "Helvetica", "Impact", "Jokerman", "King", "Kootenay", "Latha", "Liberation Serif", "Lucida Console", "Lalit", "Lucida Grande", "Magneto", "Mistral", "Modena", "Monotype Corsiva", "MV Boli", "OCR A Extended", "Onyx", "Palatino Linotype", "Papyrus", "Parchment", "Pericles", "Playbill", "Segoe Print", "Shruti", "Tahoma", "TeX", "Times", "Times New Roman", "Trebuchet MS", "Verdana", "Verona"];
        var c = "";
        for (i = 0; i < a.length; ++i) {
            var b = this.test(a[i]);
            if (b.found) {
                c += b.name + ","
            }
        }
        return c.slice(0, - 1)
    }
};

Open in new window


Please help me how to document.write() to Display Fonts list.
0
Comment
Question by:chrisvee
10 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39271064
Where do you want to display it - in what format? List, free form text, dropdown select?
0
 
LVL 43

Expert Comment

by:Rob
ID: 39271069
That's not code for detecting fonts on a computer.  It compares the fonts width listed in the code against "sans".  Not only would it be incomplete but its not 100% accurate.
What are you trying to achieve by this?
0
 

Author Comment

by:chrisvee
ID: 39271072
in browser. Just Text. no need any thing fancy like dropdown.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:chrisvee
ID: 39271093
@tagit Actually flash solution works fine. But it wont work in mobiles. the code i posted above is modified from http://lalit.org/lab/javascript-css-font-detect
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39271106
Note you cannot perform a document.write after the page has loaded and rendered. It will wipe the page completely

http://jsfiddle.net/mplungjan/TwDdt/

<div id="output"></div>

window.onload=function() {
    document.getElementById("output").innerHTML=Detector.getFontList();
}

Result on my box

cursive,monospace,serif,sans-serif,fantasy,default,Arial,Arial Black,Arial Narrow,Book Antiqua,Bookman Old Style,Calibri,Century,Century Gothic,Comic Sans MS,Consolas,Courier,Courier New,Garamond,Georgia,Helvetica,Impact,Jokerman,Latha,Lucida Console,Magneto,Mistral,Monotype Corsiva,Onyx,Palatino Linotype,Parchment,Playbill,Shruti,Tahoma,Trebuchet MS,Verdana
0
 

Author Comment

by:chrisvee
ID: 39274067
how to send those font list to database (php/mysql)  through javascript ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39274075
Using jQuery and ajax:


$(function() {
    $.post("whichfonts.php?fonts="+encodeURI(Detector.getFontList()),function(data){
      alert('Font list sent to server');
    });
});
0
 

Author Comment

by:chrisvee
ID: 39274126
i implemented javascript with ajax.please check mistakes.

function send(s){
	s = s.toString(" ");

	send = "f="+encodeURIComponent(Detector.getFontList());
	ajax("page.php",send);
}

function ajax(url, send){
	var ar = null;
	try{
		ar=new XMLHttpRequest();
	}catch(e){
		if(window.ActiveXObject){
			var ax=['Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP'],i=ax.length;
			while(--i){
				try{
					ar=new ActiveXObject(ax[i]);
					break;
				}catch(e){
				}
			}
		}
		if(ar==null){
			alert("Update your browser.");
		}
	}
	ar.open("POST",url,true);
	ar.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ar.onreadystatechange=function(){
		if(ar.readyState==4){
			if(ar.status==200||ar.status==304){
				document.body.innerHTML=ar.responseText.replace(/\n/g, "<br>");
			};
		}
	}
	ar.send(send);
};

Open in new window


in php file, can i use  $_POST['f']  ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39274349
Yes, why not try?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39274355
Not sure if what your are doing with the "s" var
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question