在javascript/jquery IE与Chrome和FF中读取@font-face css
我试图读入一个javascript变量,即包含@font-face的(本地)样式表的CSS。 我设法让它在FF en Chrome中工作,但在IE或Safari中却不行 这是我的例程(另请参见--在不同的浏览器中尝试) 函数getFonts(obj,doc){ //列出所有@font-face项的cssText。 var o=obj |{}, sheets=doc.styleSheets, i=板材长度,j,规则;在javascript/jquery IE与Chrome和FF中读取@font-face css,javascript,jquery,css,font-face,Javascript,Jquery,Css,Font Face,我试图读入一个javascript变量,即包含@font-face的(本地)样式表的CSS。 我设法让它在FF en Chrome中工作,但在IE或Safari中却不行 这是我的例程(另请参见--在不同的浏览器中尝试) 函数getFonts(obj,doc){ //列出所有@font-face项的cssText。 var o=obj |{}, sheets=doc.styleSheets, i=板材长度,j,规则; 虽然(0颠倒检查工作表[i]。规则和工作表[i]。cssRules-IE两者都匹
虽然(0颠倒检查
工作表[i]。规则和工作表[i]。cssRules
-IE两者都匹配,但CSSFONFTACHERULE
仅在后者中找到(在IE9+中测试)
函数getFonts(obj,doc){
//Maakt een lijst遇到了css van alle@font-face项目。
var o=obj |{},
sheets=doc.styleSheets,
规则=null,
i=板材长度,j;
你可以使用BrowserStack.com在其他操作系统上进行测试。先生,你是我今天的英雄。我知道这可能很简单,但这很简单……哈哈!
function getFonts (obj, doc) {
// Makes a list of the cssText of all @font-face items.
var o = obj || {},
sheets = doc.styleSheets,
i = sheets.length, j, rules;
while( 0 <= --i ){
rules = sheets[i].rules || sheets[i].cssRules || [];
j = rules.length;
while( 0 <= --j ){
// Alternatives for the if below:
// - (worked only in Chrome): if ( rule[j].constructor.name === 'CSSFontFaceRule' )
// - if ( rule[j].style.cssText && rule[j].style.cssText.match('src:') )
// IE just gives NO [object CSSFontFaceRule] !!!
if( rules[j].toString() == "[object CSSFontFaceRule]" ) {
// rule.style.fontFamily works in Chrome chrome; Not in FF. For IE I don't know yet.
fontFamily = rules[j].style.fontFamily || rules[j].style.cssText.match(/font-family\s*:\s*([^;\}]*)\s*[;}]/i)[1]
// To prevent duplicates we use the cssText as key
o[ fontFamily ] = o[ fontFamily ] || {} ;
o[ fontFamily ][rules[j].style.cssText] = rules[j].style.cssText ;
}
}
}
return o;
}
function getFonts (obj, doc) {
// Maakt een lijst met de css van alle @font-face items.
var o = obj || {},
sheets = doc.styleSheets,
rule = null,
i = sheets.length, j;
while( 0 <= --i ){
rules = sheets[i].cssRules || sheets[i].rules || [];
j = rules.length;
while( 0 <= --j ){
// Alternatives for the if below:
// - if ( rule[j].style.cssText && rule[j].style.cssText.match('src:') )
// - (worked only in Chrome): if ( rule[j].constructor.name === 'CSSFontFaceRule' )
// IE just gives NO [object CSSFontFaceRule] !!!
if( rules[j].toString() == "[object CSSFontFaceRule]" ) {
// rule.style.fontFamily works in Chrome chrome; Not in FF. For IE I don't know yet.
fontFamily = rules[j].style.fontFamily || rules[j].style.cssText.match(/font-family\s*:\s*([^;\}]*)\s*[;}]/i)[1]
// To prevent duplicates we use the cssText as key
o[ fontFamily ] = o[ fontFamily ] || {} ;
o[ fontFamily ][rules[j].style.cssText] = rules[j].style.cssText ;
}
}
}
return o;
}
console.log(getFonts(null, document));