我們在我們的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