iframe自适应高度完美方法,兼容浏览器

同域、子页面高度不会动态增加:

(function(){
			
		    var iframe = document.getElementById("contentIframe"),//这里填iframeID
		    
		        setIframeHeight = function(){
		        
		            var frameContent = iframe.contentWindow.document,
		            
		                frameHeight = Math.max(frameContent.body.scrollHeight,frameContent.documentElement.scrollHeight);

		            iframe.height = frameHeight;
		        };
		    if(iframe.addEventListener){//Mozilla safari......
			    
		    	iframe.addEventListener("load",setIframeHeight,false);
		        
		    }else if(iframe.attachEvent){//IE等浏览器
			    
		    	iframe.attachEvent("onload",setIframeHeight);
		    }
		    else{
		    	iframe.onload=setIframeHeight;
			}
		})();

 

你可能感兴趣的:(浏览器,IE,Safari)