A technique that works is to look at the computed style of the element. This is supported in Opera and Firefox (and I recon in safari, but haven't tested). IE (7 at least), provides a method to get a style, but it seems to be whatever was in the stylesheet, not the computed style. More details on quirksmode: Get Styles
किसी तत्व में उपयोग किए गए फ़ॉन्ट को पकड़ने के लिए यहां एक सरल कार्य है:
/**
* Get the font used for a given element
* @argument {HTMLElement} the element to check font for
* @returns {string} The name of the used font or null if font could not be detected
*/
function getFontForElement(ele) {
if (ele.currentStyle) { // sort of, but not really, works in IE
return ele.currentStyle["fontFamily"];
} else if (document.defaultView) { // works in Opera and FF
return document.defaultView.getComputedStyle(ele,null).getPropertyValue("font-family");
} else {
return null;
}
}
यदि इसके लिए सीएसएस नियम था:
#fonttester {
font-family: sans-serif, arial, helvetica;
}
फिर इसे हेल्वैटिका वापस करनी चाहिए यदि वह स्थापित है, अगर नहीं, एरियल, और आखिरकार, सिस्टम डिफ़ॉल्ट नाम sans-serif फ़ॉन्ट का नाम। ध्यान दें कि आपके सीएसएस घोषणा में फोंट का क्रम महत्वपूर्ण है।
एक दिलचस्प हैक आप यह भी कोशिश कर सकते हैं कि मशीन पर कौन से फोंट स्थापित किए गए हैं, यह जानने के लिए कई सारे फ़ॉन्ट्स के साथ कई छिपे हुए तत्वों को बनाना है। मुझे यकीन है कि कोई इस तकनीक के साथ निफ्टी फ़ॉन्ट आंकड़े एकत्र करने वाला पृष्ठ बना सकता है।