IE8 网页字体 iframe 错误解决方法 [英] IE8 web font iframe bug workarounds
问题描述
这篇博文(有点烦人的页面那里)(顺便说一下,那不是我的博客)描述了我昨天仅在 Internet Explorer 8 中遇到的一个奇怪的错误.该错误涉及 .EOT 网络字体和 元素.
This blog post (slightly annoying page there) (and that's not my blog by the way) describes a bizarre bug I ran into yesterday in Internet Explorer 8 only. The bug involves .EOT web fonts and <iframe>
elements.
我没有广泛研究该错误的确切触发因素,但基本上是这样的情况,即使用 Web 字体的页面将内容加载到 <iframe>
中,这样框架 also 使用的网络字体会被浏览器破坏".以前用 Web 字体渲染的 OK 文本突然变成了看起来很糟糕的 Arial 或其他东西,有点像它自己.有时它会翻转回来,只是在随机用户交互(如鼠标移动)时再次降级.
I haven't extensively researched the exact trigger for the bug, but it's basically the case that a page using a web font that loads content into an <iframe>
such that the frame also uses a web font becomes "defaced" by the browser. The previously-OK text rendered with the web font suddenly shifts to awful-looking Arial or something else, sort-of on its own. Sometimes it flips back, only to degrade again on random user interactions like mouse moves.
那篇博文有一个例子.澄清一下,是包含页面搞砸了,而不是 <iframe>
中的页面(至少,目前我的经验是这样).
That blog post has an example. To clarify, it's the containing page that gets messed up, not the page in the <iframe>
(at least, that's the case so far in my experience).
有没有人找到比该博客中建议的更好的解决方法,即强制重新加载"CSS <link>
元素,@font-face
声明来了?(我可以这样做,但它会有点痛苦,而且它会迫使我将字体设置移出我的文档 <head>
,如果我记得这是一个性能问题;我'我必须四处寻找,再次找到那个花絮.)
Has anybody found a better workaround than what's suggested in that blog, which is to force a "reload" of the CSS <link>
element from whence the @font-face
declarations come? (I could do that but it'd be a minor pain, plus it would force me to move my font setup out of my document <head>
which if I recall is a performance issue; I'll have to scrounge around and find that tidbit again.)
编辑 —更新
好的这是一个测试页面.这是主(容器)页面:
OK here's a test page. Here's the main (container) page:
<!DOCTYPE html>
<html>
<head>
<style id='font_style'>
@font-face {
font-family: 'bold-display';
src: url('DejaVuSans-Bold.eot');
}
</style>
<style>
.fancy { font-family: bold-display, "franklin gothic medium", "verdana", sans-serif; font-size: 32px; }
iframe { width: 500px; height: 200px; }
#floater {
position: absolute;
top: 100px; left: 100px;
display: none;
}
#floater.showing {
display: block;
}
</style>
<script>
function load() {
var frame = document.createElement('iframe'),
floater = document.getElementById('floater'),
target = document.getElementById('target');
frame.src = 'frame.html';
target.appendChild(frame);
floater.className += 'showing';
}
function unload() {
var floater = document.getElementById('floater'),
target = document.getElementById('target');
target.innerHTML = '';
floater.className = floater.className.replace(/showing/g, '');
}
</script>
</head>
<body>
<div class='fancy'>Hello World</div>
<button type='button' onclick='load()'>Click Me</button>
<div id='floater'>
<div id='target'></div>
<button type='button' onclick='unload()'>Close</button>
</body>
</html>
框架页面具有相同的 @font-face
和一个虚拟消息.
The frame page has the same @font-face
and a dummy message.
该问题似乎与使用加载的字体以及包含多个替代字体的列表有关.我(无缘无故)在我的字体系列"值中加入了一些类似的更常见的字体.当我把它们放回:
The problem appears to have something to do with using the loaded fonts with a list of more than one alternate font. I (for no good reason) had tossed in a couple of similar more-common fonts in my "font-family" values. When I dropped them back to:
.title { font-family: bold-display, sans-serif; }
然后问题就消失了(或者至少到目前为止它似乎已经消失了).
then the problem went away (or at least it seems to have gone away so far).
感谢那些提供帮助的人.给@albert,添加一个总结您尝试过的内容的答案,我会为您点赞:-)
Thanks to those who've helped out. To @albert, add an answer summarizing what you tried and I'll upvote you :-)
推荐答案
因此,以下内容不会影响性能(只要您的 CSS 大小合理),您就可以离开<link>
标签在 中,它可以正常工作,但您基本上仍然在重新加载"您的
> 元素(尽管您不是通过重置它们的 url 来实现的).
So, there is no performance hit with the following (as long as your CSS is reasonably sized), you get to leave the <link>
tag in <head>
, and it works without issue, but you are still basically 'reloading' your <link>
elements (though you are not doing so by resetting their url).
关于删除 元素,只需执行以下操作:
On the removal of the <iframe>
element, simply do the following:
var sheets = document.styleSheets;
for(var s = 0, slen = sheets.length; s < slen; s++) {
sheets[s].disabled = true;
sheets[s].disabled = false;
}
重新加载是我真正能想到的工作,因为它似乎正在从 的垃圾收集中删除它.设置一个测试页面,显然只适用于 IE 8.
Reloading is all I can really think of as working since it seems to be removing it in garbage collection from the <iframe>
. Set up a test page that obviously only works for IE 8.
注意:我最初无法使用 Google 网络字体重现此问题,为此我不得不专门下载 .eot
字体.因此,您的解决方法可能是先使用 WOFF 字体,然后仅在必要时加载 EOT.
Of Note: I was originally unable to reprodcue this issue using Google web fonts, and had to specifically download a .eot
font for use for this. So your work around maybe to use WOFF fonts first, and only load EOT if necessary.
不确定这是否是您要查找的内容,但如果不是,请说明清楚,我会根据需要进行编辑.
Not exactly sure if this is what you were looking for, but if it's not, please clarify and I'll edit as necessary.
更新 1:原因
所以,我已经缩小了问题的原因.我只有在发生以下情况时才能重现(这是一个令人讨厌的人).
So, I've narrowed down the cause of the issue. I am only able to reproduce if the following occurs (and this is a nasty one folks).
- 并且
包含在父元素中
- 父元素的类发生了变化
- 类不会改变它被应用到的元素的
display
(或者实际上,如果它没有改变<iframe>
元素)
- And
<iframe>
is contained within a parent element - The parent element's class is changed
- The class does not change the
display
of the element it is being applied to (or really, if it does not change the overall display on the<iframe>
element)
而且,据我所知,是的,它必须是类名.如果对元素的 id
执行相同的步骤,我无法重现.严重地.这是一个讨厌的.我会继续挖一点.
And, from what I can tell, yes, it has to be the class name. I was unable to reproduce given the same steps on the id
of an element. Seriously. This one is a nasty one. I'll keep digging a bit.
更新 2:次要原因
如果绘制时未完全在浏览器窗口中,则不会加载
窗口内的字体,并将卸载主窗口在必须重绘页面时的字体(尤其是在调整页面大小时).这是一个棘手的错误.
If the <iframe>
is not fully in the browser window on draw, it will not load the font within the <iframe>
window, and will unload the font for the main window whenever it has to redraw the page (most notably when it is resized). This is a gnarly bug.
更新 3:应该是什么解决方案
不使用display:none;
,将元素的高度和宽度设置为0px
和overflow: hidden;
,您将获得与 display none 相同的效果,但它不会将其从 DOM 中删除,因此不会重新绘制页面并使用您的字体.我之前的测试页面已更新,因此您可以看到对以前的错误的影响.
Instead of using display: none;
, set the height and the width of the element to be 0px
and the overflow: hidden;
, and you'll get the same effect as display none, but it will not remove it from the DOM, thereby not redrawing the page and taking your font. My test page from before has been updated, so you can see the effect on what used to be the bug.
这篇关于IE8 网页字体 iframe 错误解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!