본문 바로가기

Web Application/HTML5

[HTML] iFrame 높이 자동 조절

출처 - http://th.atguy.com/mycode/iframe_size/


- IFrame이 이제 구시대적인 스킬이 되어가고 있지만, 아직도 많이 사용 중.
- IFrame으로 개발하던 경험상 항상 필요하던 스킬이 iFrame의 높이를 동적으로 조절해 주는 스킬 정리.

[HTML] iFrame 높이 자동 조절

<script language="JavaScript">
	function iframeAutoResize(h){
		if(h == null){
			return false;
		}
		
		(h).height = "0px";
	    var iframeHeight= (h).contentWindow.document.body.scrollHeight;
	    (h).height=iframeHeight + 15;
	}
</script>

<iframe width="700" id="the_iframe" onLoad="iframeAutoResize(this);" scrolling="NO" 	frameborder="1" 	height="1">
</iframe>
		

 - h를 0px로 초기화 하지 않으면 iFrame의 크기가 커지기는 하지만 축소가 되지 않는 문제점이 발생한다.
 - h가 null일 경우를 대비해 null 처리를 해주었다.



  - iFrame으로 코딩 시 영문도 모르게 테두리가 자동적으로 생기는 경우가 있다.
<iframe src='http://주소' width="가로" height="세로" frameborder=0 framespacing=0 marginheight=0 marginwidth=0 scrolling=no vspace=0>
</iframe >

  - 이러한 옵션을 주면 테두리를 없앨 수 있다.
  - 스크롤을 없애고 싶을 때는 scrolling = no, 아닐 땐 scrolling = yes 를 해 주면 된다.