我正在构建一个内容是 iframe 的对话框。当对话框打开时,我从各种来源中提取了以下代码以尝试使其工作:
var iframeDOM = document.createElement("IFRAME");
iframeDOM.setAttribute("frameborder", "0");
iframeDOM.setAttribute("scrolling", plugin.settings.scrolling ? "" : "no");
iframeDOM.setAttribute("allowtransparency", isIE ? "true" : "");
var setDimensions = function(){
console.log("load");
var iframeWin = iframeDOM.contentWindow || iframeDOM.contentDocument.parentWindow;
if (iframeWin.document.body) {
console.log("new height: " + iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight); //Not giving real height, always 0
console.log("new client height: " + iframeWin.document.documentElement.clientHeight);
iframeDOM.height = 550;
iframeDOM.width = 550;
}
};
$(iframeDOM).load(setDimensions)
.appendTo($element.children().first())
.attr({ src: plugin.settings.href, name: new Date().getTime() });
如您所见,我试图记录 iFrame 的高度,但它始终打印 0(作为临时解决方法,我正在静态设置尺寸)
编辑:我更新此 OP 的另一个解决方案返回错误:
Error: Permission denied to access property 'document' if (iframeWin.document.body) {