Mil*_*les 4 iframe resize dynamic
所以我一直在寻找所有其他Stack Overflow帖子和谷歌周围关于如何在iFrame上自动设置高度/宽度.到目前为止,我已经完成了大约15-20个,但没有一个对我有效.让我试着解释一下我的情况:
我正在我的页面上动态设置iFrame的主体.我需要iFrame相应地自动设置其高度和宽度,以便所有文本都可见.
我需要这个在IE(7和8),FireFox 3和Chrome中工作.
以下是进入该问题的其他问题:
我在同一个域上写所有内容,所以这不是问题.
我觉得我现在拥有的只是一个装备,随时都会崩溃.它在IE中显示正确但在FireFox中具有巨大的底部边距,并且在Chrome中根本不显示(doc始终为null).
这是(我试着尽可能详细,如果我需要解释更多,请告诉我):
<script type="text/javascript">
function WriteIFrame()
{
// get the text i need to put in the iFrame (this is set from the server)
var iFrameContent = document.getElementById("<%= hiddenIFrameContent.ClientID %>").value;
var iFrameContainer = document.getElementById("divIFrameContainer");
// create the new iFrame object
var iFrame = document.createElement("iframe");
iFrame.setAttribute("id", "myIFrame");
iFrame.setAttribute("scrolling", "no");
iFrame.setAttribute("frameborder", "0");
// add the new iFrame object to the container div
iFrameContainer.appendChild(iFrame);
// find the correct inner document of the iFrame
var doc = iFrame.document;
if (doc == null && iFrame.contentDocument)
doc = iFrame.contentDocument;
//
// write the information into the iFrame
if (doc != null)
{
doc.open();
doc.writeln(iFrameContent);
doc.close();
}
// set the proper height
var height = doc.body.scrollHeight + iFrameContainer.offsetTop;
iFrame.setAttribute("style", "width: 100%; height: " + height + "px;");
}
</script>
<div id="divIFrameContainer" oninit="WriteIFrame();">
</div>
<iframe id="HelperIFrame" style="display: none;" onload="WriteIFrame();"></iframe>
<textarea id="hiddenIFrameContent" runat="server" style="display: none;" />
Run Code Online (Sandbox Code Playgroud)
Welllll,经过几个小时的讨论,我终于开始工作了.
所以我明确表示,Chrome存在时间问题.如果在页面加载时动态设置iFrame的内容,则必须等待几毫秒才能正确设置高度.这就是为什么我使用setTimeout函数并且它每次都适用于所有浏览器的原因,如果你没有,有时Chrome会是原来的两倍.
这是我用来使其在IE,FF和Chrome中运行的代码:
<script type="text/javascript">
function OnIFrameLoad()
{
_frame = document.createElement("iframe");
_frame.setAttribute("scrolling", "auto");
_frame.setAttribute("frameborder", "0");
_frame.setAttribute("style", "width: 100%;");
_frame.style.width = "100%";
document.getElementById("IFrameContainer").appendChild(_frame);
_innerDoc = _frame.document;
if (_frame.contentDocument)
_innerDoc = _frame.contentDocument; // For NS6
if (_frame.contentWindow)
_innerDoc = _frame.contentWindow.document; // For IE5.5 and IE6
//
window.parent.SetIFrameContent();
// We're calling the ResizeIFrame function after 10 milliseconds because when
// Chrome shows the iframe, it takes a few moments to get the correct height.
setTimeout("window.parent.ResizeIFrame()", 10);
}
function SetIFrameContent()
{
var content = document.getElementById("<%= hiddenIFrameContent.ClientID %>").value;
_innerDoc.open();
_innerDoc.writeln(content);
_innerDoc.close();
}
function ResizeIFrame(frameId)
{
var objectToResize = (_frame.style) ? _frame.style : _frame;
var innerDocBody = _innerDoc.body;
var newHeight = _innerDoc.body.clientHeight;
if (_innerDoc.body.scrollHeight > newHeight)
newHeight = _innerDoc.body.scrollHeight;
//
objectToResize.height = newHeight + 40 + "px";
}
</script>
Run Code Online (Sandbox Code Playgroud)
ASP方面:
<textarea id="hiddenIFrameContent" runat="server" style="display:none;" />
<div id="IFrameContainer"></div>
Run Code Online (Sandbox Code Playgroud)