웹마스터 팁

참고하세요...
a.com이라는 도메인에서 b.com의 b.html을 불러올때.. 크기를 자동으로 조절하기 위한 방법입니다.

먼저 a라는 홈페이지에 auto_size.html을 만듭니다.
====================== auto_size.html  - a.com =========================
<html>
  <head>
    <title>Resizing Page</title>
    <script type="text/javascript">
      function onLoad() {
        var params = window.location.search.substring( 1 ).split( '&' );
        var height;
        for( var i = 0, l = params.length; i < l; ++i ) {
          var parts = params[i].split( '=' );
          switch( parts[0] ) {
          case 'height':
            height = parseInt( parts[1] );
            break;
          }
        }
        if( typeof( height ) == 'number' ) {
          window.top.updateIFrame( height );
        }
      }

    window.onload = onLoad;
    </script>
  </head>
  <body>
    <p>Resizing IFrame...</p>
  </body>
</html>
=======================================================

그리고 a라는 홈페이지에서 링크할 문서에는 아래와 같이 삽입합니다.
======================= a.html - a.com ===================
 <script type="text/javascript">
      function updateIFrame( height ) {
        var iframe = document.getElementById( 'myiframe' );
        iframe.setAttribute( '+ 'height', height );
      }
    </script>
 <iframe   id="myiframe" src="http://b.com/b.html" scrolling=no frameborder=0 width="600" height="600"></iframe>
 
이제 b라는 도메인의 b.html에는 아래와 같이 삽입합니다.
================== b.html  - b.com============================
<style>
  #inneriframe { position : absolute; top : 0px; left : 0px; visibility: hidden; }  
</style>

    <script type="text/javascript">
      function rsize() {
       var iframe = document.getElementById( 'inneriframe' );
        var wrapper = document.getElementById( 'wrapper' );
        var height = Math.max( document.body.offsetHeight, document.body.scrollHeight );
        iframe.src = 'http://a.com/auto_size.html?height='+height;      }
     </script>

<body onload=rsize(); >
<div id=wrapper>
   내용
</div>
  <iframe id="inneriframe" width="10" height="10"></iframe>
</body>