父页面获取子页面的高度,并给iframe设置高度

<iframe id="box" src="index.html" width="100%" height=""></iframe>

function setIframeHeight(id){
				    try{
				        var iframe = document.getElementById(id);
				        if(iframe.attachEvent){
				            iframe.attachEvent("onload", function(){
				                iframe.height =  iframe.contentWindow.document.documentElement.scrollHeight;
				            });
				            return;
				        }else{
				            iframe.onload = function(){
				            	setTimeout(function () {},400)
				                iframe.height = iframe.contentDocument.body.scrollHeight;
				            };
				            return;                 
				        }     
				    }catch(e){
				        throw new Error('setIframeHeight Error');
				    }
				}
				setIframeHeight('box');

 

猜你喜欢

转载自blog.csdn.net/qq_21262357/article/details/78416956