1. 同域iframe高度自适应
setSameDomainIframeHeight: function (ifmId) {
var ifm = document.getElementById(ifmId);
var ifmHeight = ifm.contentDocument.body.scrollHeight;
ifm.height = ifmHeight;
}
2. 跨域iframe高度自适应
setCrossDomainIfrmHeight: function (ifmId) {
var myEventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var myEventListener = window[myEventMethod];
var myEventMessage = myEventMethod == "attachEvent" ? "onmessage" : "message";
//----接收消息
myEventListener(myEventMessage, function (e) {
debugger;
console.log("e=", e);
if (e.data === parseInt(e.data))
document.getElementById(ifmId).height = e.data + "px";
}, false);
//----iframe加载完毕向父窗体发送消息
document.getElementById(ifmId).onload = function () {
console.log("window.parent", window.parent);
window.postMessage(document.body.scrollHeight, "*");
/* window.parent.postMessage(document.body.scrollHeight, "*");*/
};
}