我们在我们的iframe中添加了页面,将内容附加到页面上,然后允许用户刷新页面内的内容。在这些情况下,我们看到了同样的情况,在适当的情况下内容不会缩小。
FB.Canvas。的setSize()调用_computeContentSize,其如下所示:
_computeContentSize: function() {
var body = document.body,
docElement = document.documentElement,
right = 0,
bottom = Math.max(
Math.max(body.offsetHeight, body.scrollHeight) +
body.offsetTop,
Math.max(docElement.offsetHeight, docElement.scrollHeight) +
docElement.offsetTop);
if (body.offsetWidth < body.scrollWidth) {
right = body.scrollWidth + body.offsetLeft;
} else {
FB.Array.forEach(body.childNodes, function(child) {
var childRight = child.offsetWidth + child.offsetLeft;
if (childRight > right) {
right = childRight;
}
});
}
if (docElement.clientLeft > 0) {
right += (docElement.clientLeft * 2);
}
if (docElement.clientTop > 0) {
bottom += (docElement.clientTop * 2);
}
return {height: bottom, width: right};
},
问题的行是在这里:
bottom = Math.max(
Math.max(body.offsetHeight, body.scrollHeight) +
body.offsetTop,
Math.max(docElement.offsetHeight, docElement.scrollHeight) +
docElement.offsetTop);
即使你的iframe里面的内容已今非昔比,body.offsetHeight的价值不会缩水。
为了解决这个问题,我们做了computeContentSize功能的定制版本,只有咨询的docElement的高度,就像这样:
function rfComputeContentSize() {
var body = document.body,
docElement = document.documentElement,
right = 0,
bottom = Math.max(docElement.offsetHeight, docElement.scrollHeight) + docElement.offsetTop;
if (body.offsetWidth < body.scrollWidth) {
right = body.scrollWidth + body.offsetLeft;
} else {
FB.Array.forEach(body.childNodes, function(child) {
var childRight = child.offsetWidth + child.offsetLeft;
if (childRight > right) {
right = childRight;
}
});
}
if (docElement.clientLeft > 0) {
right += (docElement.clientLeft * 2);
}
if (docElement.clientTop > 0) {
bottom += (docElement.clientTop * 2);
}
return {height: bottom, width: right};
}
任何时候我们要调整大小和知道的内容有可能缩小,我们将使用自定义函数将内容传递给setSize(例如FB.Canvas.setSize(rfComputeContentSize())),并且无论何时我们知道内容只会增长,我们将使用标准的FB.Canvas.setSize()函数。
请注意,我们使用setAutoGrow(),我没有检查,但我假设它使用相同的函数来确定大小。我们禁止了对setAutoGrow()的调用,并且必须警惕在approrpriate时间调用setSize()。在他们的交叉https://developers.facebook.com/bugs/228704057203827
如果你是做得对,你不应该需要任何这一点。 – Bob 2011-05-05 16:11:44